|
|
|
@ -29,6 +29,7 @@ import { |
|
|
|
readJSONFile, |
|
|
|
removeCommentsFromCss, |
|
|
|
removeCommentsFromJs, |
|
|
|
replaceNames, |
|
|
|
uploadFile, |
|
|
|
zipProject |
|
|
|
} from "./helpers.js"; |
|
|
|
@ -139,18 +140,24 @@ app.get('/view/:baseView', (req, res) => { |
|
|
|
app.get('/publish', async (req, res) => { |
|
|
|
const data = await readJSONFile(path.join(projectPath, `block.json`)); |
|
|
|
|
|
|
|
data.static_files = { |
|
|
|
css: getBlockName(data.name).name + '.min.css', |
|
|
|
js: getBlockName(data.name).name + '.min.js', |
|
|
|
images: await getImagesList(path.join(projectPath, 'src', 'images')), |
|
|
|
} |
|
|
|
// Trigger build on the registry server only if the type of the unit is `foundation` or `component`.
|
|
|
|
const uploadStaticFiles = ['foundation', 'component'].includes(data.type); |
|
|
|
|
|
|
|
if (await isFileEmpty(path.join(projectPath, `src/scripts`, data.static_files.js), true)) { |
|
|
|
delete data.static_files.js; |
|
|
|
} |
|
|
|
// Prepare list of static files for the registry server.
|
|
|
|
if (uploadStaticFiles) { |
|
|
|
data.static_files = { |
|
|
|
css: getBlockName(data.name).name + '.min.css', |
|
|
|
js: getBlockName(data.name).name + '.min.js', |
|
|
|
images: await getImagesList(path.join(projectPath, 'src', 'images')), |
|
|
|
} |
|
|
|
|
|
|
|
if (await isFileEmpty(path.join(projectPath, `src/scripts`, data.static_files.js), true)) { |
|
|
|
delete data.static_files.js; |
|
|
|
} |
|
|
|
|
|
|
|
if (!data.static_files.images.length) { |
|
|
|
delete data.static_files.images; |
|
|
|
if (!data.static_files.images.length) { |
|
|
|
delete data.static_files.images; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
let responseData = { |
|
|
|
@ -183,25 +190,36 @@ app.get('/publish', async (req, res) => { |
|
|
|
await uploadFile(path.join(projectPath, 'dist.zip'), responseData.uploadBundleUrl); // Bundle
|
|
|
|
} |
|
|
|
|
|
|
|
// TODO: Upload CSS/JS/Images files only if the type of the unit is `foundation` or `component`.
|
|
|
|
if (responseData.staticFilesUrls.css) { |
|
|
|
await uploadFile( |
|
|
|
path.join(projectPath, 'src/styles', data.static_files.css), |
|
|
|
responseData.staticFilesUrls.css.uploadUrl, |
|
|
|
(data) => removeCommentsFromCss(data)); // CSS
|
|
|
|
} |
|
|
|
if (uploadStaticFiles) { |
|
|
|
if (responseData.staticFilesUrls.css) { |
|
|
|
await uploadFile( |
|
|
|
path.join(projectPath, 'src/styles', data.static_files.css), |
|
|
|
responseData.staticFilesUrls.css.uploadUrl, |
|
|
|
(content) => { |
|
|
|
if (responseData.staticFilesUrls.images) { |
|
|
|
content = replaceNames(content, data.static_files.images, responseData.staticFilesUrls.images); |
|
|
|
} |
|
|
|
|
|
|
|
removeCommentsFromCss(content) |
|
|
|
return content; |
|
|
|
}); // CSS
|
|
|
|
} |
|
|
|
|
|
|
|
if (responseData.staticFilesUrls.js) { |
|
|
|
await uploadFile( |
|
|
|
path.join(projectPath, 'src/scripts', data.static_files.js), |
|
|
|
responseData.staticFilesUrls.js.uploadUrl, |
|
|
|
(data) => removeCommentsFromJs(data)); // JS
|
|
|
|
} |
|
|
|
if (responseData.staticFilesUrls.js) { |
|
|
|
await uploadFile( |
|
|
|
path.join(projectPath, 'src/scripts', data.static_files.js), |
|
|
|
responseData.staticFilesUrls.js.uploadUrl, |
|
|
|
(data) => removeCommentsFromJs(data) |
|
|
|
); // JS
|
|
|
|
} |
|
|
|
|
|
|
|
if (responseData.staticFilesUrls.images) { |
|
|
|
for (let i = 0; i < data.static_files.images.length; i++) { |
|
|
|
await uploadFile(path.join(projectPath, 'src/images', data.static_files.images[i]), |
|
|
|
responseData.staticFilesUrls.images[i].uploadUrl); // Images
|
|
|
|
if (responseData.staticFilesUrls.images) { |
|
|
|
for (let i = 0; i < data.static_files.images.length; i++) { |
|
|
|
await uploadFile( |
|
|
|
path.join(projectPath, 'src/images', data.static_files.images[i]), |
|
|
|
responseData.staticFilesUrls.images[i].uploadUrl, |
|
|
|
); // Images
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (err) { |
|
|
|
@ -214,12 +232,14 @@ app.get('/publish', async (req, res) => { |
|
|
|
|
|
|
|
await fs.unlink(path.join(projectPath, 'dist.zip')); // Remove local bundle
|
|
|
|
|
|
|
|
// TODO: Trigger build on the registry server only if the type of the unit is `foundation` or `component`.
|
|
|
|
try { |
|
|
|
await triggerGlobalProjectFilesBuild(getBlockName(data.name).project); |
|
|
|
} catch (err) { |
|
|
|
res.json({success: false, message: 'Something wrong with Project Builder.'}); |
|
|
|
return; |
|
|
|
// Trigger project's global files build on the registry server.
|
|
|
|
if (uploadStaticFiles) { |
|
|
|
try { |
|
|
|
await triggerGlobalProjectFilesBuild(getBlockName(data.name).project); |
|
|
|
} catch (err) { |
|
|
|
res.json({success: false, message: 'Something wrong with Project Builder.'}); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
res.json({success: true}); |
|
|
|
|