@platform/ts
Version:
TypesScript build, prepare and publish toolchain.
268 lines (267 loc) • 8.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var yargs = require("yargs");
var common_1 = require("../common");
var cmds = require("./cmds");
process.on('unhandledRejection', function (err) {
throw err;
});
var CMD = {
BUILD: 'build',
BUILD_B: 'b',
LINT: 'lint',
LINT_L: 'l',
TEST: 'test',
TEST_T: 't',
PREPARE: 'prepare',
PREPARE_P: 'p',
PUBLISH: 'publish',
PUBLISH_PUB: 'pub',
CHMOD: 'chmod',
};
var CMDS = Object.keys(CMD)
.map(function (key) { return CMD[key]; })
.map(function (cmd) { return cmd.split(' ')[0]; });
var DESCRIPTION = {
SILENT: 'Suppress console output',
};
var program = yargs
.scriptName('')
.usage('Usage: ts <command> [options]')
.command([CMD.BUILD, CMD.BUILD_B], 'Transpile typescript', function (e) {
return e
.option('silent', {
alias: 's',
describe: DESCRIPTION.SILENT,
boolean: true,
})
.option('watch', {
alias: 'w',
describe: 'Watch for changes',
boolean: true,
})
.option('dir', {
describe: 'The directory of the module',
string: true,
})
.option('outDir', {
describe: 'Redirect output structure to the directory',
string: true,
})
.option('tsconfig', {
describe: 'Name of the `tsconfig` file',
string: true,
})
.option('no-esm', {
describe: 'Do not include ESModule output (.jsm)',
boolean: true,
});
}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var silent, watch, dir, outDir, esm, tsconfig, formats, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
silent = e.silent, watch = e.watch, dir = e.dir, outDir = e.outDir, esm = e.esm, tsconfig = e.tsconfig;
if (!watch) return [3, 2];
return [4, cmds.build({ silent: silent, tsconfig: tsconfig, dir: dir, outDir: outDir, watch: true, as: 'COMMON_JS' })];
case 1:
_a.sent();
return [3, 4];
case 2:
formats = [];
formats = esm !== false ? tslib_1.__spreadArray(tslib_1.__spreadArray([], formats, true), ['ES_MODULE'], false) : formats;
formats = tslib_1.__spreadArray(tslib_1.__spreadArray([], formats, true), ['COMMON_JS'], false);
return [4, cmds.buildAs(formats, {
silent: silent,
watch: watch,
tsconfig: tsconfig,
dir: dir,
outDir: outDir,
})];
case 3:
res = _a.sent();
if (res.error) {
fail(1, res.error);
}
_a.label = 4;
case 4: return [2];
}
});
}); })
.command([CMD.LINT, CMD.LINT_L], 'Run linter', function (e) {
return e
.option('silent', {
alias: 's',
describe: DESCRIPTION.SILENT,
boolean: true,
})
.option('dir', {
describe: 'The directory of the module',
string: true,
});
}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var silent, dir, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
silent = e.silent, dir = e.dir;
return [4, cmds.lint({ silent: silent, dir: dir })];
case 1:
res = _a.sent();
if (res.error) {
fail(1, res.error);
}
return [2];
}
});
}); })
.command([CMD.TEST, CMD.TEST_T], "Run tests", function (e) {
return e
.option('silent', {
alias: 's',
describe: DESCRIPTION.SILENT,
boolean: true,
})
.option('dir', {
describe: 'The directory of the module',
string: true,
})
.option('watch', {
alias: 'w',
describe: 'Watch for changes',
boolean: true,
})
.option('suffix', {
describe: 'The test file suffix to match.',
default: 'test,TEST',
string: true,
});
}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var silent, watch, dir, suffix, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
silent = e.silent, watch = e.watch, dir = e.dir, suffix = e.suffix;
return [4, cmds.test({ silent: silent, watch: watch, dir: dir, suffix: suffix })];
case 1:
res = _a.sent();
if (res.error) {
fail(1, res.error);
}
return [2];
}
});
}); })
.command([CMD.PREPARE, CMD.PREPARE_P], "Prepare for publish", function (e) {
return e
.option('silent', {
alias: 's',
describe: DESCRIPTION.SILENT,
boolean: true,
})
.option('dir', {
describe: 'The directory of the module',
string: true,
});
}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var silent, dir, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
silent = e.silent, dir = e.dir;
return [4, cmds.prepare({ silent: silent, dir: dir })];
case 1:
res = _a.sent();
if (res.error) {
fail(1, res.error);
}
return [2];
}
});
}); })
.command([CMD.PUBLISH, CMD.PUBLISH_PUB], "Publish to NPM", function (e) {
return e
.option('silent', {
alias: 's',
describe: DESCRIPTION.SILENT,
boolean: true,
})
.option('dir', {
describe: 'The directory of the module',
string: true,
})
.option('outDir', {
describe: 'The dir typescript has been transpiled to',
string: true,
});
}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var silent, dir, outDir, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
silent = e.silent, dir = e.dir, outDir = e.outDir;
return [4, cmds.publish({ silent: silent, dir: dir, outDir: outDir })];
case 1:
res = _a.sent();
if (res.error) {
fail(1, res.error);
}
return [2];
}
});
}); })
.command([CMD.CHMOD], "Change permissions on [node_modules/.bin] files.", function (e) {
return e
.positional('permissions', {
type: 'string',
default: '777',
describe: 'The permissions to apply',
})
.option('silent', {
alias: 's',
describe: DESCRIPTION.SILENT,
boolean: true,
});
}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var silent, _a, permissions, res;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
silent = e.silent, _a = e.permissions, permissions = _a === void 0 ? '777' : _a;
return [4, cmds.chmod({ silent: silent, permissions: permissions })];
case 1:
res = _b.sent();
if (res.error) {
fail(1, res.error);
}
return [2];
}
});
}); })
.help('h')
.alias('h', 'help')
.alias('v', 'version')
.recommendCommands();
(function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var argv, command;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, program.argv];
case 1:
argv = _a.sent();
command = argv._[0];
if (!CMDS.includes(command)) {
program.showHelp('log');
common_1.log.info();
process.exit(0);
}
return [2];
}
});
}); })();
function fail(code, error) {
var message = typeof error === 'string' ? error : error.message;
common_1.log.info("\n\uD83D\uDE1E ".concat(message));
process.exit(code);
}