@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
259 lines (254 loc) • 11 kB
JavaScript
;
var chunk4LPHTBJM_cjs = require('./chunk-4LPHTBJM.cjs');
var chunkN2HLPYZL_cjs = require('./chunk-N2HLPYZL.cjs');
var chunk6FZTSUNH_cjs = require('./chunk-6FZTSUNH.cjs');
var chunkSFV4P2MX_cjs = require('./chunk-SFV4P2MX.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 getEnvPaths = require('@stryke/env/get-env-paths');
var json = require('@stryke/fs/json');
var listFiles = require('@stryke/fs/list-files');
var removeFile = require('@stryke/fs/remove-file');
var hash = require('@stryke/hash/hash');
var hashFiles = require('@stryke/hash/hash-files');
var stormJson = require('@stryke/json/storm-json');
var exists = require('@stryke/path/exists');
var filePathFns = require('@stryke/path/file-path-fns');
var getWorkspaceRoot = require('@stryke/path/get-workspace-root');
var joinPaths = require('@stryke/path/join-paths');
var resolve = require('@stryke/path/resolve');
var kebabCase = require('@stryke/string-format/kebab-case');
var uuid = require('@stryke/unique-id/uuid');
var defu = require('defu');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var defu__default = /*#__PURE__*/_interopDefault(defu);
var PROJECT_ROOT_HASH_LENGTH = 45;
function getPrefixedProjectRootHash(name, projectRootHash) {
const combined = `${kebabCase.kebabCase(name)}_${projectRootHash}`;
return combined.length > PROJECT_ROOT_HASH_LENGTH ? combined.slice(0, PROJECT_ROOT_HASH_LENGTH) : combined;
}
chunk3ONWID2V_cjs.__name(getPrefixedProjectRootHash, "getPrefixedProjectRootHash");
async function discoverTemplates(path) {
const result = await Promise.all([
listFiles.listFiles(joinPaths.joinPaths(path, "**/*.ts")),
listFiles.listFiles(joinPaths.joinPaths(path, "**/*.tsx"))
]);
return result.flat();
}
chunk3ONWID2V_cjs.__name(discoverTemplates, "discoverTemplates");
async function getChecksum(path) {
return hashFiles.hashDirectory(path, {
ignore: [
"node_modules",
".git",
".nx",
".cache",
".storm",
"tmp",
"dist"
]
});
}
chunk3ONWID2V_cjs.__name(getChecksum, "getChecksum");
async function getPersistedMeta(context) {
const metaFilePath = joinPaths.joinPaths(context.dataPath, "meta.json");
if (exists.existsSync(metaFilePath)) {
try {
return await json.readJsonFile(metaFilePath);
} catch {
context.log(types.LogLevelLabel.WARN, `Failed to read meta file at ${metaFilePath}. It may be corrupted.`);
await removeFile.removeFile(metaFilePath);
context.persistedMeta = void 0;
}
}
return void 0;
}
chunk3ONWID2V_cjs.__name(getPersistedMeta, "getPersistedMeta");
async function createContext(inlineConfig, workspaceConfig, options = {}) {
const workspaceRoot = workspaceConfig?.workspaceRoot ?? getWorkspaceRoot.getWorkspaceRoot();
const projectRoot = inlineConfig.root || getWorkspaceRoot.getProjectRoot() || process.cwd();
const resolvedWorkspaceConfig = defu__default.default(workspaceConfig, {
workspaceRoot
});
let projectJson;
const projectJsonPath = joinPaths.joinPaths(projectRoot, "project.json");
if (exists.existsSync(projectJsonPath)) {
projectJson = await json.readJsonFile(projectJsonPath);
}
let packageJson;
const packageJsonPath = joinPaths.joinPaths(projectRoot, "package.json");
if (exists.existsSync(packageJsonPath)) {
packageJson = await json.readJsonFile(packageJsonPath);
} else if (inlineConfig.command === "new") {
const workspacePackageJsonPath = joinPaths.joinPaths(workspaceRoot, "package.json");
packageJson = await json.readJsonFile(workspacePackageJsonPath);
resolvedWorkspaceConfig.repository ??= typeof packageJson?.repository === "string" ? packageJson.repository : packageJson?.repository?.url;
} else {
throw new Error(`The package.json file is missing in the project root directory: ${projectRoot}. Please run the "new" command to create a new Storm Stack project.`);
}
const checksum = await getChecksum(projectRoot);
const meta = {
buildId: uuid.uuid(),
releaseId: uuid.uuid(),
checksum,
timestamp: Date.now(),
projectRootHash: hash.hash(joinPaths.joinPaths(workspaceRoot, projectRoot), {
maxLength: PROJECT_ROOT_HASH_LENGTH
}),
runtimeIdMap: {},
virtualFiles: {}
};
const artifactsPath = joinPaths.joinPaths(workspaceRoot, projectRoot, ".storm");
const runtimePath = joinPaths.joinPaths(artifactsPath, "runtime");
const entryPath = joinPaths.joinPaths(artifactsPath, "entry");
const envPaths = getEnvPaths.getEnvPaths({
orgId: "storm-software",
appId: "storm-stack",
workspaceRoot
});
if (!envPaths.cache) {
throw new Error("The cache directory could not be determined.");
}
envPaths.cache = joinPaths.joinPaths(envPaths.cache, "projects", meta.projectRootHash);
const partiallyResolvedContext = {
options: {
...resolvedWorkspaceConfig,
name: projectJson?.name || options.name,
...inlineConfig,
userConfig: {
config: {}
},
inlineConfig,
projectRoot,
workspaceConfig: resolvedWorkspaceConfig,
plugins: {}
},
log: chunkSFV4P2MX_cjs.createLog(options.name ?? null, defu__default.default(inlineConfig, resolvedWorkspaceConfig)),
meta,
envPaths,
artifactsPath,
runtimePath,
entryPath,
dtsPath: joinPaths.joinPaths(envPaths.cache, "dts"),
runtimeDtsFilePath: joinPaths.joinPaths(projectRoot, "storm.d.ts"),
dataPath: joinPaths.joinPaths(envPaths.data, "projects", meta.projectRootHash),
cachePath: envPaths.cache,
templates: [],
projectJson,
packageJson,
runtime: {
logs: [],
storage: [],
init: []
},
packageDeps: {},
workers: {},
reflections: {},
resolver: chunk6FZTSUNH_cjs.createResolver({
workspaceRoot,
projectRoot,
cacheDir: envPaths.cache
}),
relativeToWorkspaceRoot: filePathFns.relativeToWorkspaceRoot(projectRoot)
};
partiallyResolvedContext.options = await chunkHVMPQ543_cjs.resolveConfig(partiallyResolvedContext, inlineConfig, void 0, projectRoot);
const context = partiallyResolvedContext;
context.dataPath = joinPaths.joinPaths(context.envPaths.data, "projects", getPrefixedProjectRootHash(context.options.name, context.meta.projectRootHash));
context.persistedMeta = await getPersistedMeta(context);
context.templates = (await Promise.all([
discoverTemplates(context.options.templates),
discoverTemplates(joinPaths.joinPaths(context.envPaths.config, "templates")),
discoverTemplates(joinPaths.joinPaths(projectRoot, "templates"))
])).flat();
context.runtimeDtsFilePath = 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");
context.tsconfig = chunkZ72ISLE5_cjs.getParsedTypeScriptConfig(context.options.workspaceRoot, context.options.projectRoot, context.options.tsconfig);
if (context.persistedMeta?.checksum === context.meta.checksum) {
context.log(types.LogLevelLabel.TRACE, `Restoring the virtual file system (VFS) as the meta checksum has not changed.`);
context.vfs = chunk4LPHTBJM_cjs.restoreVfs(context, {
runtimeIdMap: context.persistedMeta.runtimeIdMap,
virtualFiles: context.persistedMeta.virtualFiles
});
} else {
context.vfs = chunk4LPHTBJM_cjs.createVfs(context);
}
const packagePath = process.env.STORM_STACK_LOCAL ? joinPaths.joinPaths(context.options.workspaceRoot, "dist/packages/core") : await resolve.resolvePackage("@storm-stack/core");
if (!packagePath) {
throw new Error("Could not resolve the Storm Stack core package. Please ensure it is installed.");
}
return context;
}
chunk3ONWID2V_cjs.__name(createContext, "createContext");
function serializeContext(context) {
return {
...context,
log: null,
workers: null,
resolver: null,
compiler: null,
unimport: null,
reflections: Object.entries(context.reflections).reduce((ret, [key, reflection]) => {
ret[key] = reflection?.serializeType();
return ret;
}, {}),
tsconfig: {
tsconfigFilePath: context.tsconfig.tsconfigFilePath,
tsconfigJson: context.tsconfig.tsconfigJson
},
vfs: {
runtimeIdMap: context.vfs.runtimeIdMap.entries().reduce((ret, [key, value]) => {
ret[key] = value;
return ret;
}, {}),
virtualFiles: context.vfs[chunkN2HLPYZL_cjs.__VFS_VIRTUAL__].toJSON(context.artifactsPath)
}
};
}
chunk3ONWID2V_cjs.__name(serializeContext, "serializeContext");
function deserializeContext(serialized, logName = null) {
const context = {
...serialized,
log: chunkSFV4P2MX_cjs.createLog(logName, serialized.options),
reflections: Object.keys(serialized.reflections).reduce((ret, key) => {
ret[key] = (void 0).from((void 0)(serialized.reflections[key]));
return ret;
}, {}),
resolver: chunk6FZTSUNH_cjs.createResolver({
workspaceRoot: serialized.options.workspaceRoot,
projectRoot: serialized.options.projectRoot,
cacheDir: serialized.envPaths.cache
}),
vfs: {},
compiler: {},
tsconfig: {},
workers: {},
unimport: {}
};
context.vfs = chunk4LPHTBJM_cjs.restoreVfs(context, serialized.vfs);
context.tsconfig = chunkZ72ISLE5_cjs.getParsedTypeScriptConfig(context.options.workspaceRoot, serialized.options.projectRoot, serialized.tsconfig.tsconfigFilePath, serialized.tsconfig.tsconfigJson);
return context;
}
chunk3ONWID2V_cjs.__name(deserializeContext, "deserializeContext");
async function writeMetaFile(context) {
const metaFilePath = joinPaths.joinPaths(context.dataPath, "meta.json");
context.log(types.LogLevelLabel.DEBUG, `Writing runtime metadata to ${metaFilePath}`);
context.meta.runtimeIdMap = context.vfs.runtimeIdMap.entries().reduce((map, [id, path]) => {
map[id] = path;
return map;
}, {});
context.meta.virtualFiles = context.vfs[chunkN2HLPYZL_cjs.__VFS_VIRTUAL__].toJSON(context.artifactsPath);
return context.vfs.writeFileToDisk(metaFilePath, stormJson.StormJSON.stringify(context.meta));
}
chunk3ONWID2V_cjs.__name(writeMetaFile, "writeMetaFile");
exports.PROJECT_ROOT_HASH_LENGTH = PROJECT_ROOT_HASH_LENGTH;
exports.createContext = createContext;
exports.deserializeContext = deserializeContext;
exports.discoverTemplates = discoverTemplates;
exports.getChecksum = getChecksum;
exports.getPersistedMeta = getPersistedMeta;
exports.getPrefixedProjectRootHash = getPrefixedProjectRootHash;
exports.serializeContext = serializeContext;
exports.writeMetaFile = writeMetaFile;
//# sourceMappingURL=chunk-UTUPUYE6.cjs.map
//# sourceMappingURL=chunk-UTUPUYE6.cjs.map