Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ test/fixtures/ts/node_modules/aliyun-egg/
!test/fixtures/test-demo-app/node_modules/
!test/fixtures/test-demo-app-esm/node_modules/

!test/fixtures/test-postinstall/node_modules/
!test/fixtures/test path with space/**/node_modules/

.mochawesome-reports
run
.tmp
Expand All @@ -36,6 +39,7 @@ yarn.lock
dist
test/fixtures/example-declarations/typings/
!test/fixtures/example-declarations/node_modules
!test/fixtures/test path with space/example-declarations/node_modules/egg-ts-helper/dist/
.package-lock.json
.tshy*
.eslintcache
2 changes: 1 addition & 1 deletion scripts/postinstall.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function main() {
// https://github.com/eggjs/egg-ts-helper/pull/104
process.env.ETS_SCRIPT_FRAMEWORK = frameworkPackageName;
console.log('[@eggjs/bin/postinstall] run %s on %s', etsBinFile, npmRunRoot);
runScript(`node ${etsBinFile}`);
runScript(`node "${etsBinFile}"`);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/baseCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { debuglog } from 'node:util';
import { pathToFileURL } from 'node:url';
import path from 'node:path';
import os from 'node:os';
import { fork, ForkOptions, ChildProcess } from 'node:child_process';
import { Command, Flags, Interfaces } from '@oclif/core';
import { importResolve } from '@eggjs/utils';
Expand Down Expand Up @@ -261,7 +262,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
paths: findPaths,
});
debug('run ets first: %o', etsBin);
await runScript(`node ${etsBin}`);
await runScript(`node "${etsBin}"`);
}

if (this.pkgEgg.revert) {
Expand Down Expand Up @@ -327,9 +328,13 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {

protected formatImportModule(modulePath: string) {
if (this.isESM) {
return `--import ${pathToFileURL(modulePath).href}`;
return `--import "${pathToFileURL(modulePath).href}"`;
}
return `--require ${modulePath}`;
if (os.platform() === 'win32') {
// windows path need to escape backslash: `node --require "C:\\path\\to\\module"`
return `--require "${path.win32.normalize(modulePath).replace(/\\/g, '\\\\')}"`;
}
return `--require "${modulePath}"`;
}

protected addNodeOptions(options: string) {
Expand Down
11 changes: 9 additions & 2 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ export default class Dev<T extends typeof Dev> extends BaseCommand<T> {
const requires = await this.formatRequires();
const execArgv: string[] = [];
for (const r of requires) {
const imports = this.formatImportModule(r).split(' ');
execArgv.push(...imports);
const module = this.formatImportModule(r);

// Remove the quotes from the path
// --require "module path" -> ['--require', 'module path']
// --import "module path" -> ['--import', 'module path']
const splitIndex = module.indexOf(' ');
if (splitIndex !== -1) {
execArgv.push(module.slice(0, splitIndex), module.slice(splitIndex + 2, -1));
}
}
await this.forkNode(serverBin, args, { execArgv });
}
Expand Down
10 changes: 9 additions & 1 deletion test/commands/cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,15 @@ describe('test/commands/cov.test.ts', () => {
// .debug()
.expect('stdout', /1\) should fail/)
.expect('stdout', /1 failing/)
.expect('stderr', /exit with code 1/)
// The formatted coverage report will automatically wrap when output.
// There is a certain probability that it will be truncated.
// For example:
// ==== Coverage Summary ====
// Error: xxxxxxxxx.js exit
// with code 1
// Code: 1

// .expect('stderr', /exit with code 1/)
.expect('code', 1)
.end();
});
Expand Down
54 changes: 54 additions & 0 deletions test/commands/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,58 @@ describe('test/commands/dev.test.ts', () => {
.expect('code', 0)
.end();
});

describe('work on special path', () => {
it('should work with space in path', () => {
return coffee.fork(eggBin, [ 'dev' ], {
cwd: getFixtures('test path with space/example-app'),
})
// .debug()
.expect('stdout', /Hello, world!/)
.expect('code', 0)
.end();
});

it('should support declarations with space in path', () => {
return coffee.fork(eggBin, [ 'dev' ], {
cwd: getFixtures('test path with space/example-declarations'),
})
// .debug()
.expect('stdout', /Hi, I am Egg TS helper!/)
.expect('code', 0)
.end();
});

it('should support egg.require with space in path', () => {
return coffee.fork(eggBin, [ 'dev' ], {
cwd: getFixtures('test path with space/example-egg-require'),
})
// .debug()
.expect('stdout', /hey, you require me by --require/)
.expect('code', 0)
.end();
});

it('should support --require with space in path', () => {
return coffee.fork(eggBin, [ 'dev', '--require', getFixtures('test path with space/require script.cjs') ], {
cwd: getFixtures('test path with space/example-require-script'),
})
// .debug()
.expect('stdout', /hey, you require me by --require/)
.expect('code', 0)
.end();
});

it('should support --import with space in path', () => {
return coffee.fork(eggBin, [ 'dev', '--import', getFixtures('test path with space/require script.mjs') ], {
cwd: getFixtures('test path with space/example-import-script'),
})
// .debug()
.expect('stdout', /hey, you require me by --import/)
.expect('code', 0)
.end();
});

});

});
12 changes: 12 additions & 0 deletions test/commands/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,16 @@ describe('test/commands/test.test.ts', () => {
.end();
});
});

describe('work on special path', () => {
it('should work with space in path', () => {
return coffee.fork(eggBin, [ 'test' ], {
cwd: getFixtures('test path with space/test-files'),
})
// .debug()
.expect('stdout', /should success/)
.expect('code', 0)
.end();
});
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/fixtures/test path with space/example-app/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export default function () {
console.log('Hello, world!');
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/fixtures/test path with space/example-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "example-app",
"egg": {
"typescript": true
},
"dependencies": {
"egg": "*"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "example-declarations",
"egg": {
"declarations": true
},
"dependencies": {
"egg": "*"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "example-egg-require",
"egg": {
"require": [
"../require script.cjs"
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "example-import-script",
"type": "module"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "example-require-script"
}
1 change: 1 addition & 0 deletions test/fixtures/test path with space/require script.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hey, you require me by --require');
1 change: 1 addition & 0 deletions test/fixtures/test path with space/require script.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hey, you require me by --import');
3 changes: 3 additions & 0 deletions test/fixtures/test path with space/test-files/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "test-files"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const assert = require('assert');

describe('test', () => {
it('should success', () => {
assert(true);
});
});
8 changes: 8 additions & 0 deletions test/fixtures/test-postinstall/node_modules/egg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/test-postinstall/node_modules/egg/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/fixtures/test-postinstall/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "test-postinstall",
"egg": {
"typescript": true
},
"dependencies": {
"egg": "*"
}
}
7 changes: 7 additions & 0 deletions test/fixtures/test-postinstall/typings/app/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file is created by egg-ts-helper@3.1.1
// Do not modify this file!!!!!!!!!
/* eslint-disable */

import 'egg';
export * from 'egg';
export as namespace Egg;
34 changes: 34 additions & 0 deletions test/fixtures/test-postinstall/typings/config/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file is created by egg-ts-helper@3.1.1
// Do not modify this file!!!!!!!!!
/* eslint-disable */

import 'egg';
import '@eggjs/onerror';
import '@eggjs/session';
import '@eggjs/i18n';
import '@eggjs/watcher';
import '@eggjs/multipart';
import '@eggjs/security';
import '@eggjs/development';
import '@eggjs/logrotator';
import '@eggjs/schedule';
import '@eggjs/static';
import '@eggjs/jsonp';
import '@eggjs/view';
import { EggPluginItem } from 'egg';
declare module 'egg' {
interface EggPlugin {
onerror?: EggPluginItem;
session?: EggPluginItem;
i18n?: EggPluginItem;
watcher?: EggPluginItem;
multipart?: EggPluginItem;
security?: EggPluginItem;
development?: EggPluginItem;
logrotator?: EggPluginItem;
schedule?: EggPluginItem;
static?: EggPluginItem;
jsonp?: EggPluginItem;
view?: EggPluginItem;
}
}
Loading