fix: avoid symlinks to work on Windows (#13)

This commit is contained in:
Kristoffer K 2020-09-29 15:02:59 -07:00 committed by GitHub
parent fca4fe0a50
commit b56df30796
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 31 deletions

View File

@ -77,9 +77,14 @@ export class Engine {
if (typeof range === `undefined`)
throw new Error(`Assertion failed: Specified resolution (${locator.reference}) isn't supported by any of ${ranges.join(`, `)}`);
return await pmmUtils.installVersion(folderUtils.getInstallFolder(), locator, {
const installedLocation = await pmmUtils.installVersion(folderUtils.getInstallFolder(), locator, {
spec: definition.ranges[range],
});
return {
location: installedLocation,
spec: definition.ranges[range],
};
}
async resolveDescriptor(descriptor: Descriptor, {useCache = true}: {useCache?: boolean} = {}) {

View File

@ -75,7 +75,7 @@ export class PrepareCommand extends Command<Context> {
throw new UsageError(`Failed to successfully resolve '${spec.range}' to a valid ${spec.name} release`);
const baseInstallFolder = folderUtils.getInstallFolder();
const installFolder = await this.context.engine.ensurePackageManager(resolved);
const installSpec = await this.context.engine.ensurePackageManager(resolved);
if (this.activate)
await this.context.engine.activatePackageManager(resolved);
@ -87,7 +87,7 @@ export class PrepareCommand extends Command<Context> {
? path.join(this.context.cwd, `corepack-${resolved.name}-${resolved.reference}.tgz`)
: path.join(this.context.cwd, `corepack-${resolved.name}.tgz`);
await tar.c({gzip: true, cwd: baseInstallFolder, file: fileName}, [path.relative(baseInstallFolder, installFolder)]);
await tar.c({gzip: true, cwd: baseInstallFolder, file: fileName}, [path.relative(baseInstallFolder, installSpec.location)]);
if (this.json) {
this.context.stdout.write(`${JSON.stringify(fileName)}\n`);

View File

@ -1,10 +1,3 @@
import fs from 'fs';
import {dirname, relative} from 'path';
export async function mutex<T>(p: string, cb: () => Promise<T>) {
return await cb();
}
export async function makeShim(target: string, path: string) {
await fs.promises.symlink(relative(dirname(target), path), target, `file`);
}

View File

@ -48,8 +48,8 @@ export async function main(argv: Array<string>, context: CustomContext & Partial
if (resolved === null)
throw new UsageError(`Failed to successfully resolve '${descriptor.range}' to a valid ${descriptor.name} release`);
const installTarget = await context.engine.ensurePackageManager(resolved);
const exitCode = await pmmUtils.runVersion(installTarget, resolved, binaryName, this.proxy, this.context);
const installSpec = await context.engine.ensurePackageManager(resolved);
const exitCode = await pmmUtils.runVersion(installSpec, resolved, binaryName, this.proxy, this.context);
return exitCode;
}

View File

@ -119,22 +119,6 @@ export async function installVersion(installTarget: string, locator: Locator, {s
sendTo.on(`finish`, resolve);
});
await fs.promises.mkdir(path.join(tmpFolder, `.bin`));
if (Array.isArray(spec.bin)) {
if (outputFile !== null) {
for (const name of spec.bin) {
await fsUtils.makeShim(path.join(tmpFolder, `.bin`, name), outputFile);
}
} else {
throw new Error(`Assertion failed`);
}
} else {
for (const [name, dest] of Object.entries(spec.bin)) {
fsUtils.makeShim(path.join(tmpFolder, `.bin`, name), path.join(tmpFolder, dest));
}
}
await fs.promises.mkdir(path.dirname(installFolder), {recursive: true});
await fs.promises.rename(tmpFolder, installFolder);
@ -143,8 +127,21 @@ export async function installVersion(installTarget: string, locator: Locator, {s
});
}
export async function runVersion(installTarget: string, locator: Locator, binName: string, args: Array<string>, context: Context) {
const binPath = path.join(installTarget, `.bin`, binName);
export async function runVersion(installSpec: { location: string, spec: PackageManagerSpec }, locator: Locator, binName: string, args: Array<string>, context: Context) {
let binPath: string | null = null;
if (Array.isArray(installSpec.spec.bin)) {
binPath = path.join(installSpec.location, `${binName}.js`);
} else {
for (const [name, dest] of Object.entries(installSpec.spec.bin)) {
if (name === binName) {
binPath = path.join(installSpec.location, dest);
break;
}
}
}
if (!binPath)
throw new Error(`Assertion failed: Unable to locate bin path`);
return new Promise<number>((resolve, reject) => {
process.on(`SIGINT`, () => {
@ -162,7 +159,7 @@ export async function runVersion(installTarget: string, locator: Locator, binNam
if (context.stderr === process.stderr)
stdio[2] = `inherit`;
const sub = spawn(process.execPath, [binPath, ...args], {
const sub = spawn(process.execPath, [binPath!, ...args], {
cwd: context.cwd,
stdio,
});