Skip to content

Commit 9a9eccc

Browse files
coolyuantao张元涛43115
authored andcommitted
fix: properly quote paths
1 parent ff604ec commit 9a9eccc

File tree

36 files changed

+309
-8
lines changed

36 files changed

+309
-8
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ test/fixtures/ts/node_modules/aliyun-egg/
2020
!test/fixtures/test-demo-app/node_modules/
2121
!test/fixtures/test-demo-app-esm/node_modules/
2222

23+
!test/fixtures/test-postinstall/node_modules/
24+
!test/fixtures/test path with space/**/node_modules/
25+
2326
.mochawesome-reports
2427
run
2528
.tmp
@@ -36,6 +39,7 @@ yarn.lock
3639
dist
3740
test/fixtures/example-declarations/typings/
3841
!test/fixtures/example-declarations/node_modules
42+
!test/fixtures/test path with space/example-declarations/node_modules/egg-ts-helper/dist/
3943
.package-lock.json
4044
.tshy*
4145
.eslintcache

scripts/postinstall.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async function main() {
5252
// https://github.com/eggjs/egg-ts-helper/pull/104
5353
process.env.ETS_SCRIPT_FRAMEWORK = frameworkPackageName;
5454
console.log('[@eggjs/bin/postinstall] run %s on %s', etsBinFile, npmRunRoot);
55-
runScript(`node ${etsBinFile}`);
55+
runScript(`node "${etsBinFile}"`);
5656
}
5757
}
5858
}

src/baseCommand.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { debuglog } from 'node:util';
22
import { pathToFileURL } from 'node:url';
33
import path from 'node:path';
4+
import os from 'node:os';
45
import { fork, ForkOptions, ChildProcess } from 'node:child_process';
56
import { Command, Flags, Interfaces } from '@oclif/core';
67
import { importResolve } from '@eggjs/utils';
@@ -261,7 +262,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
261262
paths: findPaths,
262263
});
263264
debug('run ets first: %o', etsBin);
264-
await runScript(`node ${etsBin}`);
265+
await runScript(`node "${etsBin}"`);
265266
}
266267

267268
if (this.pkgEgg.revert) {
@@ -327,9 +328,12 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
327328

328329
protected formatImportModule(modulePath: string) {
329330
if (this.isESM) {
330-
return `--import ${pathToFileURL(modulePath).href}`;
331+
return `--import "${pathToFileURL(modulePath).href}"`;
331332
}
332-
return `--require ${modulePath}`;
333+
if (os.platform() === 'win32') {
334+
return `--require "${path.win32.normalize(modulePath).replace(/\\/g, '\\\\')}"`;
335+
}
336+
return `--require "${modulePath}"`;
333337
}
334338

335339
protected addNodeOptions(options: string) {

src/commands/dev.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ export default class Dev<T extends typeof Dev> extends BaseCommand<T> {
4444
const requires = await this.formatRequires();
4545
const execArgv: string[] = [];
4646
for (const r of requires) {
47-
const imports = this.formatImportModule(r).split(' ');
48-
execArgv.push(...imports);
47+
const module = this.formatImportModule(r);
48+
const splitIndex = module.indexOf(' ');
49+
if (splitIndex !== -1) {
50+
// Remove the quotes from the path
51+
// --require "module path" -> ['--require', 'module path']
52+
// --import "module path" -> ['--import', 'module path']
53+
execArgv.push(module.slice(0, splitIndex), module.slice(splitIndex + 2, -1));
54+
}
4955
}
5056
await this.forkNode(serverBin, args, { execArgv });
5157
}

test/commands/cov.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,15 @@ describe('test/commands/cov.test.ts', () => {
147147
// .debug()
148148
.expect('stdout', /1\) should fail/)
149149
.expect('stdout', /1 failing/)
150-
.expect('stderr', /exit with code 1/)
150+
// The formatted coverage report will automatically wrap when output.
151+
// There is a certain probability that it will be truncated.
152+
// For example:
153+
// ==== Coverage Summary ====
154+
// Error: xxxxxxxxx.js exit
155+
// with code 1
156+
// Code: 1
157+
158+
// .expect('stderr', /exit with code 1/)
151159
.expect('code', 1)
152160
.end();
153161
});

test/commands/dev.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,58 @@ describe('test/commands/dev.test.ts', () => {
231231
.expect('code', 0)
232232
.end();
233233
});
234+
235+
describe('work on special path', () => {
236+
it('should work with space in path', () => {
237+
return coffee.fork(eggBin, [ 'dev' ], {
238+
cwd: getFixtures('test path with space/example-app'),
239+
})
240+
// .debug()
241+
.expect('stdout', /Hello, world!/)
242+
.expect('code', 0)
243+
.end();
244+
});
245+
246+
it('should support declarations with space in path', () => {
247+
return coffee.fork(eggBin, [ 'dev' ], {
248+
cwd: getFixtures('test path with space/example-declarations'),
249+
})
250+
// .debug()
251+
.expect('stdout', /Hi, I am Egg TS helper!/)
252+
.expect('code', 0)
253+
.end();
254+
});
255+
256+
it('should support egg.require with space in path', () => {
257+
return coffee.fork(eggBin, [ 'dev' ], {
258+
cwd: getFixtures('test path with space/example-egg-require'),
259+
})
260+
// .debug()
261+
.expect('stdout', /hey, you require me by --require/)
262+
.expect('code', 0)
263+
.end();
264+
});
265+
266+
it('should support --require with space in path', () => {
267+
return coffee.fork(eggBin, [ 'dev', '--require', getFixtures('test path with space/require script.cjs') ], {
268+
cwd: getFixtures('test path with space/example-require-script'),
269+
})
270+
// .debug()
271+
.expect('stdout', /hey, you require me by --require/)
272+
.expect('code', 0)
273+
.end();
274+
});
275+
276+
it('should support --import with space in path', () => {
277+
return coffee.fork(eggBin, [ 'dev', '--import', getFixtures('test path with space/require script.mjs') ], {
278+
cwd: getFixtures('test path with space/example-import-script'),
279+
})
280+
// .debug()
281+
.expect('stdout', /hey, you require me by --import/)
282+
.expect('code', 0)
283+
.end();
284+
});
285+
286+
});
287+
234288
});

test/commands/test.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,16 @@ describe('test/commands/test.test.ts', () => {
394394
.end();
395395
});
396396
});
397+
398+
describe('work on special path', () => {
399+
it('should work with space in path', () => {
400+
return coffee.fork(eggBin, [ 'test' ], {
401+
cwd: getFixtures('test path with space/test-files'),
402+
})
403+
// .debug()
404+
.expect('stdout', /should success/)
405+
.expect('code', 0)
406+
.end();
407+
});
408+
});
397409
});

test/fixtures/example-ts-error-stack/node_modules/egg/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
export default function () {
3+
console.log('Hello, world!');
4+
}

test/fixtures/test path with space/example-app/node_modules/egg/index.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)