Skip to content

Commit

Permalink
fix: avoid symlinks to work on Windows (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz authored Sep 29, 2020
1 parent fca4fe0 commit b56df30
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
7 changes: 6 additions & 1 deletion sources/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} = {}) {
Expand Down
4 changes: 2 additions & 2 deletions sources/commands/Prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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`);
Expand Down
7 changes: 0 additions & 7 deletions sources/fsUtils.ts
Original file line number Diff line number Diff line change
@@ -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`);
}
4 changes: 2 additions & 2 deletions sources/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
35 changes: 16 additions & 19 deletions sources/pmmUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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`, () => {
Expand All @@ -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,
});
Expand Down

0 comments on commit b56df30

Please sign in to comment.