notification-js/scripts/build.ts
2020-12-01 20:35:56 +09:00

149 lines
4.4 KiB
TypeScript

import * as path from 'path';
import {execSync} from 'child_process';
import * as fse from 'fs-extra';
async function build(args: string[]): Promise<any[]> {
const rootPath = path.join(__dirname, '..');
const packPath = path.join(rootPath, 'pack');
const projectName = args[0];
const projectPath = path.join(rootPath, 'projects', projectName);
const packageJson = require(path.join(projectPath, 'package.json'));
const projectJson = require(path.join(projectPath, 'project.json'));
const tsconfigJsonPath = path.join(projectPath, 'tsconfig.lib.prod.json');
const distPath = path.join(projectPath, projectJson.dest);
const clean = () =>
new Promise<void>((resolve, reject) => {
try {
console.log(`Cleaning is started.`);
execSync(`rimraf ${distPath}`, {stdio: 'inherit'});
console.log(`Cleaning is completed.`);
resolve();
} catch (e) {
reject(e);
}
});
const declaration = () =>
new Promise<void>((resolve, reject) => {
try {
const outDir = path.join(distPath, 'types');
console.log(`Generating typescript declaration files is started.`);
execSync(
`tsc --project ${tsconfigJsonPath} --module commonjs --target es5 --declaration true --emitDeclarationOnly --outDir ${outDir}`,
{stdio: 'inherit'}
);
console.log(`Generating typescript declaration files is completed.`);
resolve();
} catch (e) {
reject(e);
}
});
const transpile = (
name: string,
dirName: string,
moduleName: string,
targetName: string
) =>
new Promise<void>((resolve, reject) => {
try {
let out = '';
switch (moduleName.toLowerCase()) {
case 'amd':
case 'system':
out = `--outFile ${path.join(distPath, dirName, 'public-api.js')}`;
break;
default:
out = `--outDir ${path.join(distPath, dirName)}`;
break;
}
const outDir = path.join(distPath, dirName);
console.log(`Generating typescript ${name} module is started.`);
execSync(
`tsc --project ${tsconfigJsonPath} --module ${moduleName} --target ${targetName} ${out}`,
{stdio: 'inherit'}
);
console.log(`Generating typescript ${name} module is completed.`);
resolve();
} catch (e) {
reject(e);
}
});
const packagejson = () =>
new Promise<void>((resolve, reject) => {
try {
console.log(`Generating project package.json is started.`);
const projectPackageJson = {
...packageJson,
main: `cjs/public-api.js`,
module: `esm5/public-api.js`,
es2015: `es2015/public-api.js`,
types: `types/public-api.d.ts`,
sideEffects: false,
};
fse.writeFileSync(
path.join(distPath, 'package.json'),
JSON.stringify(projectPackageJson, null, 2)
);
console.log(`Generating project package.json is completed.`);
resolve();
} catch (e) {
reject(e);
}
});
const install2Local = () =>
new Promise<void>((resolve, reject) => {
try {
console.log(`Installing to local is started.`);
const distPackageJson = require(path.join(distPath, 'package.json'));
const packFileName = `${projectName}-${distPackageJson.version}.tgz`;
const distFilePath = path.join(distPath, packFileName);
const packFilePath = path.join(packPath, packFileName);
process.chdir(path.join(distPath));
execSync(`npm pack`, {stdio: 'inherit'});
fse.moveSync(distFilePath, packFilePath, {
overwrite: true,
});
process.chdir(path.join(rootPath));
execSync(`npm install -D ${packFilePath}`, {
stdio: 'inherit',
});
console.log(`Installing to local is completed.`);
resolve();
} catch (e) {
reject(e);
}
});
return Promise.all([
clean(),
declaration(),
transpile('commonjs', 'cjs', 'commonjs', 'es5'),
transpile('amd', 'amd', 'amd', 'es5'),
transpile('umd', 'umd', 'umd', 'es5'),
transpile('ESM5', 'esm5', 'es2015', 'es5'),
transpile('ES2015', 'es2015', 'es2015', 'es2015'),
packagejson(),
install2Local(),
]);
}
build(process.argv.slice(2))
.then()
.catch(reason => {
console.error(reason);
});