Skip to content

Commit b56df30

Browse files
authored
fix: avoid symlinks to work on Windows (#13)
1 parent fca4fe0 commit b56df30

File tree

5 files changed

+26
-31
lines changed

5 files changed

+26
-31
lines changed

sources/Engine.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,14 @@ export class Engine {
7777
if (typeof range === `undefined`)
7878
throw new Error(`Assertion failed: Specified resolution (${locator.reference}) isn't supported by any of ${ranges.join(`, `)}`);
7979

80-
return await pmmUtils.installVersion(folderUtils.getInstallFolder(), locator, {
80+
const installedLocation = await pmmUtils.installVersion(folderUtils.getInstallFolder(), locator, {
8181
spec: definition.ranges[range],
8282
});
83+
84+
return {
85+
location: installedLocation,
86+
spec: definition.ranges[range],
87+
};
8388
}
8489

8590
async resolveDescriptor(descriptor: Descriptor, {useCache = true}: {useCache?: boolean} = {}) {

sources/commands/Prepare.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class PrepareCommand extends Command<Context> {
7575
throw new UsageError(`Failed to successfully resolve '${spec.range}' to a valid ${spec.name} release`);
7676

7777
const baseInstallFolder = folderUtils.getInstallFolder();
78-
const installFolder = await this.context.engine.ensurePackageManager(resolved);
78+
const installSpec = await this.context.engine.ensurePackageManager(resolved);
7979

8080
if (this.activate)
8181
await this.context.engine.activatePackageManager(resolved);
@@ -87,7 +87,7 @@ export class PrepareCommand extends Command<Context> {
8787
? path.join(this.context.cwd, `corepack-${resolved.name}-${resolved.reference}.tgz`)
8888
: path.join(this.context.cwd, `corepack-${resolved.name}.tgz`);
8989

90-
await tar.c({gzip: true, cwd: baseInstallFolder, file: fileName}, [path.relative(baseInstallFolder, installFolder)]);
90+
await tar.c({gzip: true, cwd: baseInstallFolder, file: fileName}, [path.relative(baseInstallFolder, installSpec.location)]);
9191

9292
if (this.json) {
9393
this.context.stdout.write(`${JSON.stringify(fileName)}\n`);

sources/fsUtils.ts

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
import fs from 'fs';
2-
import {dirname, relative} from 'path';
3-
41
export async function mutex<T>(p: string, cb: () => Promise<T>) {
52
return await cb();
63
}
7-
8-
export async function makeShim(target: string, path: string) {
9-
await fs.promises.symlink(relative(dirname(target), path), target, `file`);
10-
}

sources/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export async function main(argv: Array<string>, context: CustomContext & Partial
4848
if (resolved === null)
4949
throw new UsageError(`Failed to successfully resolve '${descriptor.range}' to a valid ${descriptor.name} release`);
5050

51-
const installTarget = await context.engine.ensurePackageManager(resolved);
52-
const exitCode = await pmmUtils.runVersion(installTarget, resolved, binaryName, this.proxy, this.context);
51+
const installSpec = await context.engine.ensurePackageManager(resolved);
52+
const exitCode = await pmmUtils.runVersion(installSpec, resolved, binaryName, this.proxy, this.context);
5353

5454
return exitCode;
5555
}

sources/pmmUtils.ts

+16-19
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,6 @@ export async function installVersion(installTarget: string, locator: Locator, {s
119119
sendTo.on(`finish`, resolve);
120120
});
121121

122-
await fs.promises.mkdir(path.join(tmpFolder, `.bin`));
123-
124-
if (Array.isArray(spec.bin)) {
125-
if (outputFile !== null) {
126-
for (const name of spec.bin) {
127-
await fsUtils.makeShim(path.join(tmpFolder, `.bin`, name), outputFile);
128-
}
129-
} else {
130-
throw new Error(`Assertion failed`);
131-
}
132-
} else {
133-
for (const [name, dest] of Object.entries(spec.bin)) {
134-
fsUtils.makeShim(path.join(tmpFolder, `.bin`, name), path.join(tmpFolder, dest));
135-
}
136-
}
137-
138122
await fs.promises.mkdir(path.dirname(installFolder), {recursive: true});
139123
await fs.promises.rename(tmpFolder, installFolder);
140124

@@ -143,8 +127,21 @@ export async function installVersion(installTarget: string, locator: Locator, {s
143127
});
144128
}
145129

146-
export async function runVersion(installTarget: string, locator: Locator, binName: string, args: Array<string>, context: Context) {
147-
const binPath = path.join(installTarget, `.bin`, binName);
130+
export async function runVersion(installSpec: { location: string, spec: PackageManagerSpec }, locator: Locator, binName: string, args: Array<string>, context: Context) {
131+
let binPath: string | null = null;
132+
if (Array.isArray(installSpec.spec.bin)) {
133+
binPath = path.join(installSpec.location, `${binName}.js`);
134+
} else {
135+
for (const [name, dest] of Object.entries(installSpec.spec.bin)) {
136+
if (name === binName) {
137+
binPath = path.join(installSpec.location, dest);
138+
break;
139+
}
140+
}
141+
}
142+
143+
if (!binPath)
144+
throw new Error(`Assertion failed: Unable to locate bin path`);
148145

149146
return new Promise<number>((resolve, reject) => {
150147
process.on(`SIGINT`, () => {
@@ -162,7 +159,7 @@ export async function runVersion(installTarget: string, locator: Locator, binNam
162159
if (context.stderr === process.stderr)
163160
stdio[2] = `inherit`;
164161

165-
const sub = spawn(process.execPath, [binPath, ...args], {
162+
const sub = spawn(process.execPath, [binPath!, ...args], {
166163
cwd: context.cwd,
167164
stdio,
168165
});

0 commit comments

Comments
 (0)