@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
404 lines (392 loc) • 19.3 kB
JavaScript
;
var chunkRWO2P75L_cjs = require('./chunk-RWO2P75L.cjs');
var chunkN2HLPYZL_cjs = require('./chunk-N2HLPYZL.cjs');
var chunkCV2HBLLG_cjs = require('./chunk-CV2HBLLG.cjs');
var chunkEND3JFY3_cjs = require('./chunk-END3JFY3.cjs');
var chunk3IE55BNO_cjs = require('./chunk-3IE55BNO.cjs');
var chunkHVMPQ543_cjs = require('./chunk-HVMPQ543.cjs');
var chunkZ72ISLE5_cjs = require('./chunk-Z72ISLE5.cjs');
var chunk3ONWID2V_cjs = require('./chunk-3ONWID2V.cjs');
var types = require('@storm-software/config-tools/types');
var parseTypeDefinition = require('@stryke/convert/parse-type-definition');
var isSetString = require('@stryke/type-checks/is-set-string');
var hash = require('@stryke/hash/hash');
var joinPaths = require('@stryke/path/join-paths');
var superdiff = require('@donedeal0/superdiff');
var json = require('@stryke/fs/json');
var packageFns = require('@stryke/fs/package-fns');
var stormJson = require('@stryke/json/storm-json');
var titleCase = require('@stryke/string-format/title-case');
var chalk = require('chalk');
var tsconfig = require('@stryke/fs/tsconfig');
var filePathFns = require('@stryke/path/file-path-fns');
var ts = require('typescript');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var chalk__default = /*#__PURE__*/_interopDefault(chalk);
var ts__default = /*#__PURE__*/_interopDefault(ts);
function resolveEntry(options, entry) {
const parsed = parseTypeDefinition.parseTypeDefinition(entry);
const entryFile = parsed.file.replace(options.options.projectRoot, "").replaceAll("\\", "/").replaceAll(/^(?:\.\/)*/g, "");
return {
file: joinPaths.joinPaths(options.artifactsPath, `entry-${hash.hash({
file: entryFile,
name: parsed.name
}, {
maxLength: 24
}).replaceAll("-", "0").replaceAll("_", "1")}.ts`),
name: parsed.name
};
}
chunk3ONWID2V_cjs.__name(resolveEntry, "resolveEntry");
// src/commands/init/entry/index.ts
async function initEntry(context, hooks) {
context.log(types.LogLevelLabel.TRACE, `Initializing the entry points for the Storm Stack project.`);
if (context.options.projectType === "application" && context.options.entry) {
if (isSetString.isSetString(context.options.entry)) {
context.entry = [
{
...resolveEntry(context, context.options.entry),
input: parseTypeDefinition.parseTypeDefinition(context.options.entry)
}
];
} else if (Array.isArray(context.options.entry) && context.options.entry.filter(Boolean).length > 0) {
context.entry = context.options.entry.map((entry) => ({
...resolveEntry(context, entry),
input: parseTypeDefinition.parseTypeDefinition(entry)
})).filter(Boolean);
}
}
await hooks.callHook("init:entry", context).catch((error) => {
context.log(types.LogLevelLabel.ERROR, `An error occurred while initializing the entry points for the Storm Stack project: ${error.message}
${error.stack ?? ""}`);
throw new Error("An error occurred while initializing the entry points for the Storm Stack project", {
cause: error
});
});
}
chunk3ONWID2V_cjs.__name(initEntry, "initEntry");
// src/commands/init/install/node.ts
function getNodeDeps(context) {
context.packageDeps ??= {};
context.packageDeps["@types/node"] = "devDependency";
if (context.options.projectType === "application") {
context.packageDeps.unctx = "dependency";
}
return context.packageDeps;
}
chunk3ONWID2V_cjs.__name(getNodeDeps, "getNodeDeps");
// src/commands/init/install/shared.ts
function getSharedDeps(context) {
context.packageDeps ??= {};
context.packageDeps["@storm-stack/types"] = "devDependency";
if (context.options.projectType === "application") {
context.packageDeps.unstorage = "dependency";
}
return context.packageDeps;
}
chunk3ONWID2V_cjs.__name(getSharedDeps, "getSharedDeps");
// src/commands/init/install/index.ts
async function initInstall(context, hooks) {
context.log(types.LogLevelLabel.TRACE, `Checking and installing missing project dependencies.`);
context.packageDeps = getSharedDeps(context);
if (context.options.platform === "node") {
const installs = getNodeDeps(context);
context.packageDeps = Object.keys(installs).reduce((ret, key) => {
if (installs[key] && ret[key] !== "dependency") {
ret[key] = installs[key];
}
return ret;
}, context.packageDeps);
}
await hooks.callHook("init:install", context).catch((error) => {
context.log(types.LogLevelLabel.ERROR, `An error occured while installing project dependencies: ${error.message}
${error.stack ?? ""}`);
throw new Error("An error occured while installing project dependencies", {
cause: error
});
});
context.log(types.LogLevelLabel.TRACE, `The following packages must be installed as dependencies:
${Object.keys(context.packageDeps).map((key) => ` - ${key} (${context.packageDeps[key]})`).join("\n")}`);
for (const [key, value] of Object.entries(context.packageDeps)) {
await chunkRWO2P75L_cjs.installPackage(context, key, value === "devDependency");
}
}
chunk3ONWID2V_cjs.__name(initInstall, "initInstall");
async function initOptions(context, hooks) {
context.log(types.LogLevelLabel.TRACE, `Initializing the processing options for the Storm Stack project.`);
if (context.packageJson) {
if (context.options.command === "new") {
context.options.repository ??= typeof context.packageJson.repository === "string" ? context.packageJson.repository : context.packageJson.repository?.url;
} else {
if (context.packageJson?.name) {
context.options.name ??= context.packageJson?.name;
}
context.options.description ??= context.packageJson?.description;
context.options.repository ??= typeof context.packageJson?.repository === "string" ? context.packageJson.repository : context.packageJson?.repository?.url;
}
} else if (context.options.command !== "new") {
throw new Error(`The package.json file is missing in the project root directory: ${context.options.projectRoot}. Please run the "new" command to create a new Storm Stack project.`);
}
if (context.projectJson) {
context.options.projectType ??= context.projectJson.projectType;
context.options.name ??= context.projectJson.name;
if (context.options.name?.startsWith("@") && context.options.name.split("/").filter(Boolean).length > 1) {
context.options.name = context.options.name.split("/").filter(Boolean)[1];
}
}
context.options.tsconfig ??= joinPaths.joinPaths(context.options.projectRoot, "tsconfig.json");
context.options.esbuild.override ??= {};
context.options.external ??= [];
context.options.noExternal ??= [];
context.options.esbuild.target ??= "esnext";
context.options.esbuild.format ??= "esm";
context.options.environment ??= chunkHVMPQ543_cjs.defaultEnvironmentName(context.options);
await hooks.callHook("init:options", context).catch((error) => {
context.log(types.LogLevelLabel.ERROR, `An error occurred while initializing the options for the Storm Stack project: ${error.message}
${error.stack ?? ""}`);
throw new Error("An error occurred while initializing the options for the Storm Stack project", {
cause: error
});
});
context.log(types.LogLevelLabel.TRACE, "Initialized the processing options for the Storm Stack project.");
}
chunk3ONWID2V_cjs.__name(initOptions, "initOptions");
async function initReflections(context, hooks) {
context.log(types.LogLevelLabel.TRACE, "Initializing the reflections for the Storm Stack project.");
await hooks.callHook("init:reflections", context).catch((error) => {
context.log(types.LogLevelLabel.ERROR, `An error occurred while initializing the reflections for the Storm Stack project: ${error.message}
${error.stack ?? ""}`);
throw new Error("An error occurred while initializing the reflections for the Storm Stack project", {
cause: error
});
});
context.log(types.LogLevelLabel.TRACE, "Initialized the reflections for the Storm Stack project.");
}
chunk3ONWID2V_cjs.__name(initReflections, "initReflections");
async function getTsconfigChanges(context) {
const tsconfig$1 = chunkZ72ISLE5_cjs.getParsedTypeScriptConfig(context.options.workspaceRoot, context.options.projectRoot, context.options.tsconfig, context.options.tsconfigRaw);
const tsconfigFilePath = chunkZ72ISLE5_cjs.getTsconfigFilePath(context.options.projectRoot, context.options.tsconfig);
const tsconfigJson = await json.readJsonFile(tsconfigFilePath);
tsconfigJson.compilerOptions ??= {};
const extendedTsconfig = await tsconfig.loadTsConfig(tsconfigFilePath);
extendedTsconfig.compilerOptions ??= {};
if (tsconfigJson.reflection !== true) {
tsconfigJson.reflection = true;
}
if (tsconfig$1.options.experimentalDecorators !== true) {
tsconfigJson.compilerOptions.experimentalDecorators = true;
}
if (tsconfig$1.options.emitDecoratorMetadata !== true) {
tsconfigJson.compilerOptions.emitDecoratorMetadata = true;
}
if (context.options.output.dts) {
const dtsFilePath = context.options.output.dts ? context.options.output.dts.startsWith(context.options.workspaceRoot) ? context.options.output.dts : joinPaths.joinPaths(context.options.workspaceRoot, context.options.output.dts) : joinPaths.joinPaths(context.options.workspaceRoot, context.options.projectRoot, "storm.d.ts");
const dtsRelativePath = joinPaths.joinPaths(filePathFns.relativePath(joinPaths.joinPaths(context.options.workspaceRoot, context.options.projectRoot), filePathFns.findFilePath(dtsFilePath)), filePathFns.findFileName(dtsFilePath));
if (!tsconfigJson.include?.some((filePattern) => chunkZ72ISLE5_cjs.isIncludeMatchFound(filePattern, [
dtsFilePath,
dtsRelativePath,
"storm.d.ts"
]))) {
tsconfigJson.include ??= [];
tsconfigJson.include.push(dtsRelativePath.startsWith("./") ? dtsRelativePath.slice(2) : dtsRelativePath);
}
}
if (!tsconfig$1.options.lib?.some((lib) => [
"lib.esnext.d.ts",
"lib.es2021.d.ts",
"lib.es2022.d.ts",
"lib.es2023.d.ts"
].includes(lib.toLowerCase()))) {
tsconfigJson.compilerOptions.lib ??= [];
tsconfigJson.compilerOptions.lib.push("esnext");
}
if (tsconfig$1.options.module !== ts__default.default.ModuleKind.ESNext) {
tsconfigJson.compilerOptions.module = "ESNext";
}
if (!tsconfig$1.options.target || ![
ts__default.default.ScriptTarget.ESNext,
ts__default.default.ScriptTarget.ES2024,
ts__default.default.ScriptTarget.ES2023,
ts__default.default.ScriptTarget.ES2022,
ts__default.default.ScriptTarget.ES2021
].includes(tsconfig$1.options.target)) {
tsconfigJson.compilerOptions.target = "ESNext";
}
if (tsconfig$1.options.moduleResolution !== ts__default.default.ModuleResolutionKind.Bundler) {
tsconfigJson.compilerOptions.moduleResolution = "Bundler";
}
if (tsconfig$1.options.moduleDetection !== ts__default.default.ModuleDetectionKind.Force) {
tsconfigJson.compilerOptions.moduleDetection = "force";
}
if (tsconfig$1.options.allowSyntheticDefaultImports !== true) {
tsconfigJson.compilerOptions.allowSyntheticDefaultImports = true;
}
if (tsconfig$1.options.noImplicitOverride !== true) {
tsconfigJson.compilerOptions.noImplicitOverride = true;
}
if (tsconfig$1.options.noUncheckedIndexedAccess !== true) {
tsconfigJson.compilerOptions.noUncheckedIndexedAccess = true;
}
if (tsconfig$1.options.skipLibCheck !== true) {
tsconfigJson.compilerOptions.skipLibCheck = true;
}
if (tsconfig$1.options.resolveJsonModule !== true) {
tsconfigJson.compilerOptions.resolveJsonModule = true;
}
if (tsconfig$1.options.isolatedModules !== true) {
tsconfigJson.compilerOptions.isolatedModules = true;
}
if (tsconfig$1.options.verbatimModuleSyntax !== false) {
tsconfigJson.compilerOptions.verbatimModuleSyntax = false;
}
if (tsconfig$1.options.allowJs !== true) {
tsconfigJson.compilerOptions.allowJs = true;
}
if (tsconfig$1.options.esModuleInterop !== true) {
tsconfigJson.compilerOptions.esModuleInterop = true;
}
if (tsconfig$1.options.declaration !== true) {
tsconfigJson.compilerOptions.declaration = true;
}
if (context.options.platform === "browser") {
if (tsconfig$1.options.jsx !== ts__default.default.JsxEmit.ReactJSX) {
tsconfigJson.compilerOptions.jsx = "react-jsx";
}
if (!tsconfig$1.options.lib?.some((lib) => lib.toLowerCase() !== "dom")) {
tsconfigJson.compilerOptions.lib ??= [];
tsconfigJson.compilerOptions.lib.push("dom");
}
if (!tsconfig$1.options.lib?.some((lib) => lib.toLowerCase() !== "dom.iterable")) {
tsconfigJson.compilerOptions.lib ??= [];
tsconfigJson.compilerOptions.lib.push("dom.iterable");
}
} else if (context.options.platform === "node") {
if (!tsconfig$1.options.types?.some((type) => type.toLowerCase() === "node" || type.toLowerCase() === "@types/node")) {
tsconfigJson.compilerOptions.types ??= [];
tsconfigJson.compilerOptions.types.push("node");
}
}
return tsconfigJson;
}
chunk3ONWID2V_cjs.__name(getTsconfigChanges, "getTsconfigChanges");
// src/commands/init/tsconfig/index.ts
async function initTsconfig(context, hooks) {
context.log(types.LogLevelLabel.TRACE, "Initializing TypeScript configuration for the Storm Stack project.");
if (!packageFns.isPackageExists("typescript")) {
throw new Error('The TypeScript package is not installed. Please install the package using the command: "npm install typescript --save-dev"');
}
const originalTsconfigJson = await json.readJsonFile(context.options.tsconfig);
const json$1 = await getTsconfigChanges(context);
await chunkEND3JFY3_cjs.writeFile(context.log, context.options.tsconfig, stormJson.StormJSON.stringify(json$1));
context.tsconfig = chunkZ72ISLE5_cjs.getParsedTypeScriptConfig(context.options.workspaceRoot, context.options.projectRoot, context.options.tsconfig, context.options.tsconfigRaw);
await hooks.callHook("init:tsconfig", context).catch((error) => {
context.log(types.LogLevelLabel.ERROR, `An error occured while resolving the TypeScript options: ${error.message}
${error.stack ?? ""}`);
throw new Error("An error occured while resolving the TypeScript options", {
cause: error
});
});
const tsconfigFilePath = chunkZ72ISLE5_cjs.getTsconfigFilePath(context.options.projectRoot, context.options.tsconfig);
const updateTsconfigJson = await json.readJsonFile(tsconfigFilePath);
if (updateTsconfigJson?.compilerOptions?.types && Array.isArray(updateTsconfigJson.compilerOptions.types) && !updateTsconfigJson.compilerOptions.types.length) {
delete updateTsconfigJson.compilerOptions.types;
}
const result = superdiff.getObjectDiff(originalTsconfigJson, updateTsconfigJson, {
ignoreArrayOrder: true,
showOnly: {
statuses: [
"added",
"deleted",
"updated"
],
granularity: "deep"
}
});
const changes = [];
const getChanges = /* @__PURE__ */ chunk3ONWID2V_cjs.__name((difference, property) => {
if (difference.status === "added" || difference.status === "deleted" || difference.status === "updated") {
if (difference.diff) {
for (const diff of difference.diff) {
getChanges(diff, property ? `${property}.${difference.property}` : difference.property);
}
} else {
changes.push({
field: property ? `${property}.${difference.property}` : difference.property,
status: difference.status,
previous: difference.status === "added" ? "---" : stormJson.StormJSON.stringify(difference.previousValue),
current: difference.status === "deleted" ? "---" : stormJson.StormJSON.stringify(difference.currentValue)
});
}
}
}, "getChanges");
for (const diff of result.diff) {
getChanges(diff);
}
if (changes.length > 0) {
context.log(types.LogLevelLabel.WARN, `Updating the following configuration values in "${tsconfigFilePath}" file:
${changes.map((change, i) => `${chalk__default.default.bold.whiteBright(`${i + 1}. ${titleCase.titleCase(change.status)} the ${change.field} field: `)}
${chalk__default.default.red(` - Previous: ${change.previous} `)}
${chalk__default.default.green(` - Updated: ${change.current} `)}
`).join("\n")}
`);
}
await chunkEND3JFY3_cjs.writeFile(context.log, tsconfigFilePath, stormJson.StormJSON.stringify(updateTsconfigJson));
context.tsconfig = chunkZ72ISLE5_cjs.getParsedTypeScriptConfig(context.options.workspaceRoot, context.options.projectRoot, context.options.tsconfig);
if (!context.tsconfig) {
throw new Error("Failed to parse the TypeScript configuration file.");
}
}
chunk3ONWID2V_cjs.__name(initTsconfig, "initTsconfig");
// src/commands/init/index.ts
async function init(context, hooks) {
await hooks.callHook("init:begin", context).catch((error) => {
context.log(types.LogLevelLabel.ERROR, `An error occured while starting initialization for the Storm Stack project: ${error.message}
${error.stack ?? ""}`);
throw new Error("An error occured while starting initialization for the Storm Stack project", {
cause: error
});
});
context.unimport = chunkCV2HBLLG_cjs.createUnimport(context);
await context.unimport.init();
await initOptions(context, hooks);
await initInstall(context, hooks);
await initTsconfig(context, hooks);
const handlePreTransform = /* @__PURE__ */ chunk3ONWID2V_cjs.__name(async (context2, sourceFile) => {
await hooks.callHook("build:pre-transform", context2, sourceFile).catch((error) => {
context2.log(types.LogLevelLabel.ERROR, `An error occured while pre-transforming the Storm Stack project: ${error.message}
${error.stack ?? ""}`);
throw new Error("An error occured while pre-transforming the Storm Stack project", {
cause: error
});
});
return sourceFile;
}, "handlePreTransform");
const handlePostTransform = /* @__PURE__ */ chunk3ONWID2V_cjs.__name(async (context2, sourceFile) => {
await hooks.callHook("build:post-transform", context2, sourceFile).catch((error) => {
context2.log(types.LogLevelLabel.ERROR, `An error occured while post-transforming the Storm Stack project: ${error.message}
${error.stack ?? ""}`);
throw new Error("An error occured while post-transforming the Storm Stack project", {
cause: error
});
});
return sourceFile;
}, "handlePostTransform");
context.compiler = new chunk3IE55BNO_cjs.Compiler(context, {
onPreTransform: handlePreTransform,
onPostTransform: handlePostTransform
});
await initEntry(context, hooks);
await initReflections(context, hooks);
await hooks.callHook("init:complete", context).catch((error) => {
context.log(types.LogLevelLabel.ERROR, `An error occured while finishing initialization for the Storm Stack project: ${error.message}
${error.stack ?? ""}`);
throw new Error("An error occured while finishing initialization for the Storm Stack project", {
cause: error
});
});
context.vfs[chunkN2HLPYZL_cjs.__VFS_INIT__]();
}
chunk3ONWID2V_cjs.__name(init, "init");
exports.init = init;
//# sourceMappingURL=chunk-T5XYNP42.cjs.map
//# sourceMappingURL=chunk-T5XYNP42.cjs.map