@knighted/duel
Version:
TypeScript dual packages.
112 lines (111 loc) • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = void 0;
const node_util_1 = require("node:util");
const node_path_1 = require("node:path");
const promises_1 = require("node:fs/promises");
const get_tsconfig_1 = require("get-tsconfig");
const read_package_up_1 = require("read-package-up");
const util_js_1 = require("./util.cjs");
const init = async (args) => {
let parsed = null;
try {
const { values } = (0, node_util_1.parseArgs)({
args,
options: {
project: {
type: 'string',
short: 'p',
default: 'tsconfig.json',
},
'target-extension': {
type: 'string',
short: 'x',
default: '',
},
'pkg-dir': {
type: 'string',
short: 'k',
},
modules: {
type: 'boolean',
short: 'm',
default: false,
},
dirs: {
type: 'boolean',
short: 'd',
default: false,
},
help: {
type: 'boolean',
short: 'h',
default: false,
},
},
});
parsed = values;
}
catch (err) {
(0, util_js_1.logError)(err.message);
return false;
}
if (parsed.help) {
(0, util_js_1.log)('Usage: duel [options]\n');
(0, util_js_1.log)('Options:');
(0, util_js_1.log)("--project, -p [path] \t Compile the project given the path to its configuration file, or to a folder with a 'tsconfig.json'.");
(0, util_js_1.log)('--pkg-dir, -k [path] \t The directory to start looking for a package.json file. Defaults to --project directory.');
(0, util_js_1.log)('--modules, -m \t\t Transform module globals for dual build target. Defaults to false.');
(0, util_js_1.log)('--dirs, -d \t\t Output both builds to directories inside of outDir. [esm, cjs].');
(0, util_js_1.log)('--help, -h \t\t Print this message.');
}
else {
const { project, 'target-extension': targetExt, 'pkg-dir': pkgDir, modules, dirs, } = parsed;
let configPath = (0, node_path_1.resolve)(project);
let stats = null;
let pkg = null;
if (targetExt) {
(0, util_js_1.logError)('--target-extension is deprecated. Define "type" in your package.json instead and the dual build will be inferred from that.');
return false;
}
try {
stats = await (0, promises_1.stat)(configPath);
}
catch {
(0, util_js_1.logError)(`Provided --project '${project}' resolves to ${configPath} which is not a file or directory.`);
return false;
}
pkg = await (0, read_package_up_1.readPackageUp)({ cwd: pkgDir ?? configPath });
if (!pkg) {
(0, util_js_1.logError)('No package.json file found.');
return false;
}
if (stats.isDirectory()) {
configPath = (0, node_path_1.join)(configPath, 'tsconfig.json');
try {
stats = await (0, promises_1.stat)(configPath);
}
catch {
(0, util_js_1.logError)(`Provided --project '${project}' resolves to a directory ${(0, node_path_1.dirname)(configPath)} with no tsconfig.json.`);
return false;
}
}
if (stats.isFile()) {
const tsconfig = (0, get_tsconfig_1.parseTsconfig)(configPath);
const projectDir = (0, node_path_1.dirname)(configPath);
if (!tsconfig.compilerOptions?.outDir) {
(0, util_js_1.log)('No outDir defined in tsconfig.json. Build output will be in "dist".');
}
return {
pkg,
dirs,
modules,
tsconfig,
projectDir,
configPath,
};
}
}
return false;
};
exports.init = init;