ucap-angular/scripts/build.js

99 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2020-03-27 08:39:01 +00:00
const { execSync } = require('child_process');
const path = require('path');
const fse = require('fs-extra');
const { Bundler } = require('scss-bundle');
2020-03-31 08:30:33 +00:00
async function buildForProduction() {
2020-03-27 08:39:01 +00:00
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);
2020-03-31 08:30:33 +00:00
const packPath = path.join(rootPath, 'pack');
2020-03-27 08:39:01 +00:00
2020-03-31 08:30:33 +00:00
let packFileName;
2020-03-27 08:39:01 +00:00
const ngBuild = () => {
return new Promise(async (resolve, reject) => {
try {
execSync(`ng build ${projectName} --prod`, { stdio: 'inherit' });
2020-03-31 08:30:33 +00:00
const projectVersion = require(path.join(distPath, 'package.json'))
.version;
packFileName = `ucap-ng-${projectName}-${projectVersion}.tgz`;
2020-03-27 08:39:01 +00:00
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);
}
});
};
2020-03-31 08:30:33 +00:00
const installToLocal = () => {
2020-03-27 08:39:01 +00:00
return new Promise(async (resolve, reject) => {
try {
process.chdir(distPath);
execSync(`npm pack`, { stdio: 'inherit' });
2020-03-31 08:30:33 +00:00
fse.moveSync(
path.join(distPath, packFileName),
path.join(packPath, packFileName),
2020-03-27 08:39:01 +00:00
{
2020-03-31 08:30:33 +00:00
overwrite: true
2020-03-27 08:39:01 +00:00
}
);
2020-03-31 08:30:33 +00:00
process.chdir(path.join(rootPath));
execSync(`npm install -D ${path.join(packPath, packFileName)}`, {
stdio: 'inherit'
});
2020-03-27 08:39:01 +00:00
resolve();
} catch (error) {
reject(error);
}
});
};
await ngBuild();
await scssBundle();
await installToLocal();
}
2020-03-31 08:30:33 +00:00
buildForProduction();