| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696 |
- import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
- import path from 'node:path';
- import spawn from 'cross-spawn';
- import { parseMcodeVersion } from './release.js';
- import { isManagedMcodeInstallRoot } from './service.js';
- import type { McodeUpdateOperationOptions } from './progress.js';
- export const MCODE_INTERNAL_NPM_REGISTRY = 'https://npmmirror.example.invalid/';
- export const MCODE_PUBLIC_NPM_REGISTRY = 'https://registry.npmjs.org/';
- export const MCODE_PUBLIC_NPM_MIRROR_REGISTRY = 'https://registry.npmmirror.com/';
- const REGISTRY_FETCH_TIMEOUT_MS = 30_000;
- const MCODE_PACKAGE_BASENAME = 'code';
- const MCODE_INTERNAL_SCOPE = '@minimax';
- const MCODE_PUBLIC_SCOPE = '@minimax-ai';
- // Public packaging rewrites this marker together with the bundled package identity.
- const MCODE_EMBEDDED_PACKAGE_NAME = '@minimax-ai/code' as McodeNpmPackageName;
- export type McodeNpmDistTag = 'latest' | 'test' | 'preview';
- export type McodeNpmPackageName = '@minimax/code' | '@minimax-ai/code';
- type TuiBuildEnvironment = 'test' | 'staging' | 'prod';
- declare const __TUI_BUILD_ENV__: TuiBuildEnvironment | undefined;
- declare const __TUI_NPM_DIST_TAG__: McodeNpmDistTag | undefined;
- export type McodePackageManagerInstallSource =
- | 'npm-global'
- | 'npm-prefix'
- | 'pnpm-global'
- | 'yarn-global'
- | 'bun-global';
- export type McodeInstallSource =
- | 'managed-installer'
- | McodePackageManagerInstallSource
- | 'unsupported';
- export interface McodePackageManagerCommand {
- readonly executable: string;
- readonly args: readonly string[];
- readonly display: string;
- }
- export interface McodeNpmPrefixInstall {
- readonly executable: string;
- readonly packageName: McodeNpmPackageName;
- readonly prefix: string;
- readonly registry: string;
- }
- export type McodePackageManagerRunOptions = McodeUpdateOperationOptions;
- export interface McodeNpmDistribution {
- readonly packageName: McodeNpmPackageName;
- readonly registry: string;
- }
- export interface ResolveLatestMcodeRegistryVersionDependencies {
- readonly platform: NodeJS.Platform;
- readonly run: (command: string, args: readonly string[]) => Promise<string>;
- readonly distribution: McodeNpmDistribution;
- readonly npmExecutable: string;
- readonly environment: NodeJS.ProcessEnv;
- readonly runtimeExecutable: string;
- }
- export interface DetectMcodeInstallSourceDependencies {
- readonly installRoot: string;
- readonly platform: NodeJS.Platform;
- readonly packageRoot: () => string | undefined;
- readonly npmGlobalPrefix: () => Promise<string>;
- readonly managedInstall: (installRoot: string) => boolean;
- readonly prefixInstall: () => McodeNpmPrefixInstall | undefined;
- }
- export async function detectMcodeInstallSource(
- dependencies: Partial<DetectMcodeInstallSourceDependencies> & { installRoot: string },
- ): Promise<McodeInstallSource> {
- const platform = dependencies.platform ?? process.platform;
- const resolved: DetectMcodeInstallSourceDependencies = {
- installRoot: dependencies.installRoot,
- platform,
- packageRoot: dependencies.packageRoot ?? resolveMcodePackageRoot,
- npmGlobalPrefix:
- dependencies.npmGlobalPrefix ??
- (() => runText(platform === 'win32' ? 'npm.cmd' : 'npm', ['prefix', '--global'])),
- managedInstall: dependencies.managedInstall ?? isManagedMcodeInstallRoot,
- prefixInstall: dependencies.prefixInstall ?? resolveMcodeNpmPrefixInstall,
- };
- if (resolved.managedInstall(resolved.installRoot)) return 'managed-installer';
- if (resolved.prefixInstall()) return 'npm-prefix';
- const packageRoot = resolved.packageRoot();
- if (!packageRoot) return 'unsupported';
- const heuristic = classifyMcodeInstallPath(packageRoot);
- if (heuristic) return heuristic;
- try {
- return classifyNpmGlobalInstall(packageRoot, await resolved.npmGlobalPrefix(), platform);
- } catch {
- return 'unsupported';
- }
- }
- export function classifyMcodeInstallPath(
- packageRoot: string,
- ): McodePackageManagerInstallSource | undefined {
- const normalized = packageRoot.replaceAll('\\', '/').toLocaleLowerCase();
- if (
- /\/pnpm\/global\/(?:v11\/[^/]+|[^/]+)\/node_modules\/@minimax(?:-ai)?\/code$/u.test(normalized)
- ) {
- return 'pnpm-global';
- }
- if (
- /\/(?:\.config\/yarn|\.yarn)\/global\/node_modules\/@minimax(?:-ai)?\/code$/u.test(normalized)
- ) {
- return 'yarn-global';
- }
- if (/\/\.bun\/install\/global\/node_modules\/@minimax(?:-ai)?\/code$/u.test(normalized)) {
- return 'bun-global';
- }
- if (
- /\/lib\/node_modules\/@minimax(?:-ai)?\/code$/u.test(normalized) ||
- /\/npm\/node_modules\/@minimax(?:-ai)?\/code$/u.test(normalized)
- ) {
- return 'npm-global';
- }
- return undefined;
- }
- export function resolveMcodeNpmDistribution(
- packageName: McodeNpmPackageName = resolveMcodePackageName() ?? MCODE_EMBEDDED_PACKAGE_NAME,
- registry?: string,
- ): McodeNpmDistribution {
- if (packageName === mcodePackageName(MCODE_INTERNAL_SCOPE)) {
- const resolvedRegistry = registry ? new URL(registry).href : MCODE_INTERNAL_NPM_REGISTRY;
- if (resolvedRegistry === MCODE_INTERNAL_NPM_REGISTRY) {
- return { packageName, registry: resolvedRegistry };
- }
- throw new Error(`Unsupported MCode npm registry: ${resolvedRegistry}`);
- }
- if (packageName === mcodePackageName(MCODE_PUBLIC_SCOPE)) {
- const resolvedRegistry = registry ? new URL(registry).href : MCODE_PUBLIC_NPM_REGISTRY;
- if (
- resolvedRegistry === MCODE_PUBLIC_NPM_REGISTRY ||
- resolvedRegistry === MCODE_PUBLIC_NPM_MIRROR_REGISTRY
- ) {
- return { packageName, registry: resolvedRegistry };
- }
- throw new Error(`Unsupported MCode npm registry: ${resolvedRegistry}`);
- }
- throw new Error(`Unsupported MCode npm package: ${String(packageName)}`);
- }
- export function resolveMcodeNpmDistTag(
- environment: TuiBuildEnvironment | undefined = readEmbeddedTuiBuildEnvironment(),
- embeddedTag: McodeNpmDistTag | undefined = readEmbeddedTuiNpmDistTag(),
- ): McodeNpmDistTag {
- if (embeddedTag) return embeddedTag;
- if (environment === 'test') return 'test';
- if (environment === 'staging') return 'preview';
- return 'latest';
- }
- export function classifyNpmGlobalInstall(
- packageRoot: string,
- globalPrefix: string,
- platform: NodeJS.Platform = process.platform,
- ): McodeInstallSource {
- const normalizedRoot = normalizeResolvedPath(packageRoot, platform);
- const platformPath = platform === 'win32' ? path.win32 : path.posix;
- const packageName =
- mcodePackageNameFromPath(packageRoot) ?? mcodePackageName(MCODE_INTERNAL_SCOPE);
- const candidates =
- platform === 'win32'
- ? [platformPath.join(globalPrefix, 'node_modules', packageName)]
- : [
- platformPath.join(globalPrefix, 'lib', 'node_modules', packageName),
- platformPath.join(globalPrefix, 'node_modules', packageName),
- ];
- return candidates.some(
- (candidate) => normalizeResolvedPath(candidate, platform) === normalizedRoot,
- )
- ? 'npm-global'
- : 'unsupported';
- }
- export function buildMcodePackageManagerCommand(
- source: McodePackageManagerInstallSource,
- version: string,
- platform: NodeJS.Platform = process.platform,
- distribution: McodeNpmDistribution = resolveMcodeNpmDistribution(),
- prefixInstall?: McodeNpmPrefixInstall,
- ): McodePackageManagerCommand {
- const targetVersion = ['latest', 'preview', 'test'].includes(version)
- ? version
- : parseMcodeVersion(version);
- const target = `${distribution.packageName}@${targetVersion}`;
- const registryArgs = ['--registry', distribution.registry] as const;
- const registryDisplay = `--registry ${distribution.registry}`;
- const npmInstallArgs = [
- '--ignore-scripts=false',
- '--include=optional',
- `--allow-scripts=${distribution.packageName},better-sqlite3`,
- ];
- const npmInstallDisplay = npmInstallArgs.join(' ');
- switch (source) {
- case 'npm-prefix': {
- if (!prefixInstall) throw new Error('MCode npm prefix ownership metadata is missing.');
- if (
- prefixInstall.packageName !== distribution.packageName ||
- new URL(prefixInstall.registry).href !== distribution.registry
- ) {
- throw new Error('MCode npm prefix ownership does not match the installed package.');
- }
- return {
- executable: prefixInstall.executable,
- args: [
- 'install',
- '--global',
- '--prefix',
- prefixInstall.prefix,
- target,
- ...npmInstallArgs,
- ...registryArgs,
- ],
- display:
- `${prefixInstall.executable} install --global --prefix ${prefixInstall.prefix} ` +
- `${target} ${npmInstallDisplay} ${registryDisplay}`,
- };
- }
- case 'npm-global':
- return {
- executable: platform === 'win32' ? 'npm.cmd' : 'npm',
- args: ['install', '--global', target, ...npmInstallArgs, ...registryArgs],
- display: `npm install --global ${target} ${npmInstallDisplay} ${registryDisplay}`,
- };
- case 'pnpm-global':
- return {
- executable: platform === 'win32' ? 'pnpm.cmd' : 'pnpm',
- args: ['add', '--global', target, ...registryArgs],
- display: `pnpm add --global ${target} ${registryDisplay}`,
- };
- case 'yarn-global':
- return {
- executable: platform === 'win32' ? 'yarn.cmd' : 'yarn',
- args: ['global', 'add', target, ...registryArgs],
- display: `yarn global add ${target} ${registryDisplay}`,
- };
- case 'bun-global':
- return {
- executable: platform === 'win32' ? 'bun.exe' : 'bun',
- args: ['add', '--global', target, ...registryArgs],
- display: `bun add --global ${target} ${registryDisplay}`,
- };
- }
- }
- export async function resolveLatestMcodeRegistryVersion(
- tag: McodeNpmDistTag,
- dependencies: Partial<ResolveLatestMcodeRegistryVersionDependencies> = {},
- ): Promise<string> {
- const platform = dependencies.platform ?? process.platform;
- const distribution = dependencies.distribution ?? resolveMcodeNpmDistribution();
- const run =
- dependencies.run ??
- ((executable, args) => {
- const command = { executable, args, display: executable };
- const bound = dependencies.runtimeExecutable
- ? bindMcodeNpmCommandToRuntime(command, dependencies.runtimeExecutable)
- : command;
- return runText(bound.executable, bound.args, dependencies.environment);
- });
- const npmExecutable = dependencies.npmExecutable ?? (platform === 'win32' ? 'npm.cmd' : 'npm');
- const output = await run(npmExecutable, [
- 'view',
- `${distribution.packageName}@${tag}`,
- 'version',
- '--json',
- '--registry',
- distribution.registry,
- '--fetch-timeout',
- String(REGISTRY_FETCH_TIMEOUT_MS),
- ]);
- let version: unknown;
- try {
- version = JSON.parse(output);
- } catch {
- version = output.trim();
- }
- if (Array.isArray(version)) {
- if (version.length !== 1) {
- throw new Error('MCode registry returned an invalid latest version.');
- }
- version = version[0];
- }
- if (typeof version !== 'string') {
- throw new Error('MCode registry returned an invalid latest version.');
- }
- const parsed = parseMcodeVersion(version);
- if (tag === 'latest' && parsed.includes('-')) {
- throw new Error('MCode latest must resolve to a stable semantic version.');
- }
- return parsed;
- }
- function readEmbeddedTuiBuildEnvironment(): TuiBuildEnvironment | undefined {
- if (typeof __TUI_BUILD_ENV__ === 'undefined') return undefined;
- return __TUI_BUILD_ENV__;
- }
- function readEmbeddedTuiNpmDistTag(): McodeNpmDistTag | undefined {
- if (typeof __TUI_NPM_DIST_TAG__ === 'undefined') return undefined;
- return __TUI_NPM_DIST_TAG__;
- }
- export function bindMcodeNpmCommandToRuntime(
- command: McodePackageManagerCommand,
- runtimeExecutable: string,
- ): McodePackageManagerCommand {
- const npmExecutable = realpathSync(command.executable);
- const npmDirectory = path.dirname(npmExecutable);
- // Unix npm is normally a symlink to npm-cli.js. Windows npm.cmd prefers its
- // adjacent node.exe over PATH, so bypass that shim and invoke the JS entry.
- const candidates = [
- ...(path.basename(npmExecutable) === 'npm-cli.js' ? [npmExecutable] : []),
- path.join(npmDirectory, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
- path.join(npmDirectory, '..', 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js'),
- ];
- const npmCli = candidates.find((file) => existsSync(file) && statSync(file).isFile());
- if (!npmCli)
- throw new Error(`Cannot locate npm-cli.js for the owned npm executable: ${command.executable}`);
- return { ...command, executable: runtimeExecutable, args: [npmCli, ...command.args] };
- }
- export function createMcodeNpmRuntimeEnvironment(
- environment: NodeJS.ProcessEnv,
- runtimeExecutable: string,
- platform: NodeJS.Platform = process.platform,
- ): NodeJS.ProcessEnv {
- const platformPath = platform === 'win32' ? path.win32 : path.posix;
- const result = { ...environment };
- // Windows environment keys are case-insensitive. Keep only one PATH key so Node and
- // cross-spawn cannot choose a different inherited spelling.
- const pathKeys = Object.keys(result)
- .filter((key) => (platform === 'win32' ? key.toLowerCase() === 'path' : key === 'PATH'))
- .sort();
- const inheritedPath = result[pathKeys[0] ?? 'PATH'];
- for (const key of pathKeys) delete result[key];
- result.PATH = [platformPath.dirname(runtimeExecutable), inheritedPath]
- .filter(Boolean)
- .join(platformPath.delimiter);
- return result;
- }
- export function runMcodePackageManagerCommand(
- command: McodePackageManagerCommand,
- environment: NodeJS.ProcessEnv = process.env,
- options: McodePackageManagerRunOptions = {},
- ): Promise<void> {
- return new Promise((resolve, reject) => {
- let settled = false;
- const child = spawn(command.executable, [...command.args], {
- env: environment,
- shell: false,
- stdio: ['ignore', 'pipe', 'pipe'],
- windowsHide: true,
- });
- const stdout: Buffer[] = [];
- const stderr: Buffer[] = [];
- child.stdout?.on('data', (chunk: Buffer) => {
- stdout.push(chunk);
- notifyPackageManagerOutput(options, chunk);
- });
- child.stderr?.on('data', (chunk: Buffer) => {
- stderr.push(chunk);
- notifyPackageManagerOutput(options, chunk);
- });
- child.once('error', (error) => {
- if (settled) return;
- settled = true;
- reject(error);
- });
- child.once('exit', (code, signal) => {
- if (settled) return;
- settled = true;
- if (code === 0) {
- resolve();
- return;
- }
- const details = Buffer.concat([...stderr, ...stdout])
- .toString('utf8')
- .trim();
- reject(
- new Error(
- `${command.executable} failed (${
- signal ? `signal ${signal}` : `exit ${String(code)}`
- })${details ? `: ${details}` : ''}`,
- ),
- );
- });
- });
- }
- function notifyPackageManagerOutput(options: McodePackageManagerRunOptions, chunk: Buffer): void {
- try {
- options.onOutput?.(chunk.toString('utf8'));
- } catch {
- // Progress rendering must never change the package-manager result.
- }
- }
- export function resolveMcodePackageName(
- entryFile = process.argv[1],
- ): McodeNpmPackageName | undefined {
- return resolveMcodePackageIdentity(entryFile)?.packageName;
- }
- export function isInternalMcodePackageName(packageName: string | undefined): boolean {
- return packageName === mcodePackageName(MCODE_INTERNAL_SCOPE);
- }
- export function resolveInstalledMcodePackageVersion(
- entryFile = process.argv[1],
- ): string | undefined {
- return resolveMcodePackageIdentity(entryFile)?.version;
- }
- export function resolveMcodeNpmPrefixInstall(
- entryFile = process.argv[1],
- platform: NodeJS.Platform = process.platform,
- nodeExecutable = process.execPath,
- ): McodeNpmPrefixInstall | undefined {
- const identity = resolveMcodePackageIdentity(entryFile);
- if (!identity) return undefined;
- const platformPath = platform === 'win32' ? path.win32 : path.posix;
- const nodeModules = platformPath.dirname(platformPath.dirname(identity.packageRoot));
- if (platformPath.basename(nodeModules).toLocaleLowerCase() !== 'node_modules') return undefined;
- const nodeModulesParent = platformPath.dirname(nodeModules);
- const packagePrefix =
- platform !== 'win32' && platformPath.basename(nodeModulesParent) === 'lib'
- ? platformPath.dirname(nodeModulesParent)
- : nodeModulesParent;
- const receipt = findNpmPrefixReceipt(packagePrefix, identity.packageRoot, platform, platformPath);
- if (receipt) {
- if (receipt.packageName !== identity.packageName) return undefined;
- return receipt;
- }
- if (platformPath.basename(packagePrefix).toLocaleLowerCase() !== '.minimax-code')
- return undefined;
- const adjacentNpm = platformPath.join(
- platformPath.dirname(nodeExecutable),
- platform === 'win32' ? 'npm.cmd' : 'npm',
- );
- if (!existsSync(adjacentNpm)) return undefined;
- const distribution = resolveMcodeNpmDistribution(identity.packageName);
- return {
- executable: adjacentNpm,
- packageName: identity.packageName,
- prefix: packagePrefix,
- registry: distribution.registry,
- };
- }
- function resolveMcodePackageRoot(entryFile = process.argv[1]): string | undefined {
- return resolveMcodePackageIdentity(entryFile)?.packageRoot;
- }
- function resolveMcodePackageIdentity(
- entryFile: string | undefined,
- ): { packageName: McodeNpmPackageName; packageRoot: string; version?: string } | undefined {
- if (!entryFile) return undefined;
- let current: string;
- try {
- const resolved = realpathSync(entryFile);
- current = statSync(resolved).isDirectory() ? resolved : path.dirname(resolved);
- } catch {
- return undefined;
- }
- for (;;) {
- const manifestFile = path.join(current, 'package.json');
- if (existsSync(manifestFile)) {
- try {
- const manifest = JSON.parse(readFileSync(manifestFile, 'utf8')) as {
- name?: unknown;
- version?: unknown;
- };
- const packageName = parseMcodePackageName(manifest.name);
- if (packageName) {
- return {
- packageName,
- packageRoot: current,
- ...(typeof manifest.version === 'string' ? { version: manifest.version } : {}),
- };
- }
- } catch {
- return undefined;
- }
- }
- const parent = path.dirname(current);
- if (parent === current) return undefined;
- current = parent;
- }
- }
- function findNpmPrefixReceipt(
- packagePrefix: string,
- packageRoot: string,
- platform: NodeJS.Platform,
- platformPath: typeof path.posix | typeof path.win32,
- ): McodeNpmPrefixInstall | undefined {
- let candidate = packagePrefix;
- for (let depth = 0; depth < 3; depth += 1) {
- const receipt = readNpmPrefixReceipt(
- candidate,
- packagePrefix,
- packageRoot,
- platform,
- platformPath,
- );
- if (receipt) return receipt;
- const parent = platformPath.dirname(candidate);
- if (parent === candidate) break;
- candidate = parent;
- }
- return undefined;
- }
- function readNpmPrefixReceipt(
- prefix: string,
- packagePrefix: string,
- packageRoot: string,
- platform: NodeJS.Platform,
- platformPath: typeof path.posix | typeof path.win32,
- ): McodeNpmPrefixInstall | undefined {
- try {
- const raw = readFileSync(platformPath.join(prefix, 'install.json'), 'utf8').replace(
- /^\uFEFF/u,
- '',
- );
- const value = JSON.parse(raw) as {
- schemaVersion?: unknown;
- product?: unknown;
- updateOwner?: unknown;
- packageManager?: unknown;
- packageName?: unknown;
- registry?: unknown;
- distTag?: unknown;
- prefix?: unknown;
- npmExecutable?: unknown;
- layoutVersion?: unknown;
- releasesDirectory?: unknown;
- currentFile?: unknown;
- };
- const packageName = parseMcodePackageName(value.packageName);
- const legacyLayout = value.schemaVersion === 1 && value.layoutVersion === undefined;
- const versionedLayout =
- value.schemaVersion === 2 &&
- value.layoutVersion === 2 &&
- value.releasesDirectory === 'releases' &&
- value.currentFile === 'current';
- if (
- (!legacyLayout && !versionedLayout) ||
- value.product !== 'minimax-code' ||
- value.updateOwner !== 'npm-prefix' ||
- value.packageManager !== 'npm' ||
- !packageName ||
- typeof value.registry !== 'string' ||
- value.distTag !== 'latest' ||
- typeof value.prefix !== 'string' ||
- typeof value.npmExecutable !== 'string'
- ) {
- return undefined;
- }
- const distribution = resolveMcodeNpmDistribution(packageName, value.registry);
- if (
- normalizeResolvedPath(value.prefix, platform) !== normalizeResolvedPath(prefix, platform) ||
- new URL(value.registry).href !== distribution.registry ||
- !existsSync(value.npmExecutable)
- ) {
- return undefined;
- }
- if (
- legacyLayout &&
- normalizeResolvedPath(packagePrefix, platform) !== normalizeResolvedPath(prefix, platform)
- ) {
- return undefined;
- }
- if (versionedLayout) {
- const expectedReleasesRoot = platformPath.join(prefix, 'releases');
- const relativeRelease = platformPath.relative(expectedReleasesRoot, packagePrefix);
- if (
- !relativeRelease ||
- relativeRelease.startsWith('..') ||
- platformPath.isAbsolute(relativeRelease) ||
- relativeRelease.includes(platformPath.sep) ||
- !/^[0-9A-Za-z][0-9A-Za-z._-]*$/u.test(relativeRelease) ||
- !isResolvedPathInside(packagePrefix, packageRoot, platformPath)
- ) {
- return undefined;
- }
- }
- return {
- executable: value.npmExecutable,
- packageName,
- prefix,
- registry: distribution.registry,
- };
- } catch {
- return undefined;
- }
- }
- function isResolvedPathInside(
- root: string,
- candidate: string,
- platformPath: typeof path.posix | typeof path.win32,
- ): boolean {
- const relative = platformPath.relative(
- platformPath.resolve(root),
- platformPath.resolve(candidate),
- );
- return relative !== '' && !relative.startsWith('..') && !platformPath.isAbsolute(relative);
- }
- function parseMcodePackageName(value: unknown): McodeNpmPackageName | undefined {
- if (value === mcodePackageName(MCODE_INTERNAL_SCOPE)) return value as McodeNpmPackageName;
- if (value === mcodePackageName(MCODE_PUBLIC_SCOPE)) return value as McodeNpmPackageName;
- return undefined;
- }
- function mcodePackageName(scope: string): McodeNpmPackageName {
- return `${scope}/${MCODE_PACKAGE_BASENAME}` as McodeNpmPackageName;
- }
- function mcodePackageNameFromPath(packageRoot: string): McodeNpmPackageName | undefined {
- const normalized = packageRoot.replaceAll('\\', '/');
- const segments = normalized.split('/');
- if (segments.at(-1) !== MCODE_PACKAGE_BASENAME) return undefined;
- return parseMcodePackageName(`${segments.at(-2)}/${MCODE_PACKAGE_BASENAME}`);
- }
- function normalizeResolvedPath(value: string, platform: NodeJS.Platform): string {
- let resolved: string;
- try {
- resolved = realpathSync(value);
- } catch {
- resolved = (platform === 'win32' ? path.win32 : path.posix).resolve(value);
- }
- return platform === 'win32' ? resolved.toLocaleLowerCase() : resolved;
- }
- function runText(
- command: string,
- args: readonly string[],
- environment: NodeJS.ProcessEnv = process.env,
- ): Promise<string> {
- return new Promise((resolve, reject) => {
- let settled = false;
- const child = spawn(command, [...args], {
- env: environment,
- shell: false,
- stdio: ['ignore', 'pipe', 'pipe'],
- windowsHide: true,
- });
- const stdout: Buffer[] = [];
- const stderr: Buffer[] = [];
- child.stdout?.on('data', (chunk: Buffer) => stdout.push(chunk));
- child.stderr?.on('data', (chunk: Buffer) => stderr.push(chunk));
- child.once('error', (error) => {
- if (settled) return;
- settled = true;
- reject(error);
- });
- child.once('exit', (code, signal) => {
- if (settled) return;
- settled = true;
- if (code === 0) {
- resolve(Buffer.concat(stdout).toString('utf8').trim());
- return;
- }
- const details = Buffer.concat([...stderr, ...stdout])
- .toString('utf8')
- .trim();
- reject(
- new Error(
- `${command} failed (${
- signal ? `signal ${signal}` : `exit ${String(code)}`
- })${details ? `: ${details}` : ''}`,
- ),
- );
- });
- });
- }
|