ucap-angular/scripts/build.js
2020-03-27 17:39:01 +09:00

126 lines
2.9 KiB
JavaScript

const { execSync } = require('child_process');
const path = require('path');
const fse = require('fs-extra');
const { Bundler } = require('scss-bundle');
async function build() {
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);
let projectVersion;
const ngBuild = () => {
return new Promise(async (resolve, reject) => {
try {
execSync(`ng build ${projectName} --prod`, { stdio: 'inherit' });
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 npmPack = () => {
return new Promise(async (resolve, reject) => {
try {
process.chdir(distPath);
execSync(`npm pack`, { stdio: 'inherit' });
projectVersion = require(path.join(distPath, 'package.json')).version;
process.chdir(path.join(rootPath));
resolve();
} catch (error) {
reject(error);
}
});
};
const installToLocal = () => {
return new Promise(async (resolve, reject) => {
try {
execSync(
`npm install -D ${path.join(
distPath,
`ucap-ng-${projectName}-${projectVersion}.tgz`
)}`,
{
stdio: 'inherit'
}
);
resolve();
} catch (error) {
reject(error);
}
});
};
const clean = () => {
return new Promise(async (resolve, reject) => {
try {
execSync(
`rimraf ${path.join(
distPath,
`ucap-ng-${projectName}-${projectVersion}.tgz`
)}`,
{
stdio: 'inherit'
}
);
resolve();
} catch (error) {
reject(error);
}
});
};
await ngBuild();
await scssBundle();
await npmPack();
await installToLocal();
await clean();
}
build();