ucap-angular/scripts/build.js
richard-loafle 6935b9a69e bug fixed
2020-03-31 17:30:33 +09:00

99 lines
2.5 KiB
JavaScript

const { execSync } = require('child_process');
const path = require('path');
const fse = require('fs-extra');
const { Bundler } = require('scss-bundle');
async function buildForProduction() {
const args = process.argv.slice(2);
const projectName = args[0];
const useScssBundle =
2 === args.length && 'useScssBundle' === args[1] ? true : false;
const rootPath = path.join(__dirname, '..');
const projectPath = path.join(rootPath, 'projects', projectName);
const distPath = path.join(rootPath, 'dist', projectName);
const packPath = path.join(rootPath, 'pack');
let packFileName;
const ngBuild = () => {
return new Promise(async (resolve, reject) => {
try {
execSync(`ng build ${projectName} --prod`, { stdio: 'inherit' });
const projectVersion = require(path.join(distPath, 'package.json'))
.version;
packFileName = `ucap-ng-${projectName}-${projectVersion}.tgz`;
resolve();
} catch (error) {
reject(error);
}
});
};
const scssBundle = () => {
return new Promise(async (resolve, reject) => {
try {
if (useScssBundle) {
const config = require(path.join(
projectPath,
'scss-bundle.config.json'
));
const bundler = new Bundler(
undefined,
path.join(rootPath, config.bundlerOptions.rootDir)
);
const result = await bundler.bundle(
path.join(rootPath, config.bundlerOptions.entryFile),
config.bundlerOptions.dedupeGlobs,
config.bundlerOptions.includePaths,
config.bundlerOptions.ignoreImports
);
fse.writeFileSync(
path.join(rootPath, config.bundlerOptions.outFile),
result.bundledContent
);
}
resolve();
} catch (error) {
reject(error);
}
});
};
const installToLocal = () => {
return new Promise(async (resolve, reject) => {
try {
process.chdir(distPath);
execSync(`npm pack`, { stdio: 'inherit' });
fse.moveSync(
path.join(distPath, packFileName),
path.join(packPath, packFileName),
{
overwrite: true
}
);
process.chdir(path.join(rootPath));
execSync(`npm install -D ${path.join(packPath, packFileName)}`, {
stdio: 'inherit'
});
resolve();
} catch (error) {
reject(error);
}
});
};
await ngBuild();
await scssBundle();
await installToLocal();
}
buildForProduction();