Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
fix: invoke source map (#52)
Browse files Browse the repository at this point in the history
* fix: invoke source map

* fix: serverless-invoke tsTmpFile

* fix: tmpdir

* fix: process exit

* fix merge bug

* fix bug

* fix: fix tmp tsconfig bug

* fix: tsc incremental not work (#51)

* fix: incremental params pass

* chores: remove useless code

* fix: incremental no effect

* refactor: update clean flag

* chore: clean test code

* chore: clean test code

* chore: update yml md

Co-authored-by: Harry Chen <czy88840616@gmail.com>

* v0.2.25

* fix: invoke source map

* fix: serverless-invoke tsTmpFile

* fix: tmpdir

* fix: process exit

* fix merge bug

* fix bug

* fix: fix tmp tsconfig bug

* fix/remove conflict

* chore: update to beta midway-*

* fix aggregation deploy

* fix: combine tsconfig

* fix: ts build error

Co-authored-by: Lellansin Huang <Lellansin@gmail.com>
Co-authored-by: Harry Chen <czy88840616@gmail.com>
  • Loading branch information
3 people committed Feb 21, 2020
1 parent 0f26293 commit 9149d2a
Show file tree
Hide file tree
Showing 39 changed files with 348 additions and 108 deletions.
2 changes: 1 addition & 1 deletion packages/egg-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@midwayjs/serverless-fc-trigger": "^0.2.22",
"@midwayjs/serverless-scf-starter": "^0.2.22",
"@midwayjs/serverless-scf-trigger": "^0.2.22",
"midway-bin": "1",
"midway-bin": "beta",
"supertest": "^4.0.2"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-command-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"minimist": "^1.2.0"
},
"devDependencies": {
"midway-bin": "1"
"midway-bin": "beta"
},
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"light-generator": "^1.3.2"
},
"devDependencies": {
"midway-bin": "1"
"midway-bin": "beta"
},
"files": [
"src",
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-deploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@midwayjs/fcli-command-core": "^0.2.22"
},
"devDependencies": {
"midway-bin": "1"
"midway-bin": "beta"
},
"files": [
"src",
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-fc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"devDependencies": {
"@midwayjs/fcli-plugin-package": "^0.2.26",
"fs-extra": "^8.1.0",
"midway-bin": "1"
"midway-bin": "beta"
},
"files": [
"src",
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-invoke/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@midwayjs/serverless-invoke": "^0.2.25"
},
"devDependencies": {
"midway-bin": "1"
"midway-bin": "beta"
},
"files": [
"src",
Expand Down
2 changes: 2 additions & 0 deletions packages/faas-cli-plugin-invoke/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ export class InvokePlugin extends BasePlugin {
this.core.cli.log(JSON.stringify(result));
this.core.cli.log('');
this.core.cli.log('--------- result end --------');
process.exit();
} catch (e) {
const errorLog = this.core.cli.error || this.core.cli.log;
errorLog(e && e.message ? `[Error] ${e.message}` : e);
process.exit(1);
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"archiver": "^3.1.1",
"fs-extra": "^8.1.0",
"globby": "^10.0.1",
"midway-bin": "1"
"midway-bin": "beta"
},
"files": [
"src",
Expand Down
29 changes: 10 additions & 19 deletions packages/faas-cli-plugin-package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,31 +320,17 @@ export class PackagePlugin extends BasePlugin {
this.core.cli.log(' - Using tradition build mode');
if (this.codeAnalyzeResult.integrationProject) {
// 生成一个临时 tsconfig
const tempConfigFilePath = join(
this.servicePath,
'tsconfig_integration_faas.json'
);

await tsIntegrationProjectCompile(this.servicePath, {
sourceDir: this.options.sourceDir || 'src',
const tsConfig = await tsIntegrationProjectCompile(this.servicePath, {
buildRoot: this.midwayBuildPath,
tsCodeRoot: this.codeAnalyzeResult.tsCodeRoot,
incremental: false,
clean: true,
});
// 把临时的 tsconfig 移动进去
await move(
tempConfigFilePath,
join(this.midwayBuildPath, 'tsconfig.json'),
{
overwrite: true,
}
);
writeJSON(join(this.midwayBuildPath, 'tsconfig.json'), tsConfig);
} else {
await tsCompile(this.servicePath, {
clean: true,
tsConfigName: 'tsconfig.json',
source: this.options.sourceDir || 'src',
tsConfigName: 'tsconfig.json'
});
// copy dist to artifact directory
await move(
Expand Down Expand Up @@ -500,7 +486,7 @@ export class PackagePlugin extends BasePlugin {
const deployOrigin = this.core.service.aggregation[aggregationName]
.deployOrigin;

const allPaths = [];
const allAggred = [];
let handlers = [];
if (this.core.service.aggregation[aggregationName].functions) {
handlers = this.core.service.aggregation[aggregationName].functions
Expand All @@ -520,7 +506,10 @@ export class PackagePlugin extends BasePlugin {
if (!httpEvent || !httpEvent.http.path) {
return;
}
allPaths.push(httpEvent.http.path);
allAggred.push({
path: httpEvent.http.path,
method: httpEvent.http.method
});
if (!deployOrigin) {
// 不把原有的函数进行部署
this.core.cli.log(
Expand All @@ -536,6 +525,7 @@ export class PackagePlugin extends BasePlugin {
.filter((func: any) => !!func);
}

const allPaths = allAggred.map(aggre => aggre.path);
let currentPath = commonPrefix(allPaths);
currentPath = currentPath ? `${currentPath}/*` : '/*';
this.core.cli.log(
Expand All @@ -549,6 +539,7 @@ export class PackagePlugin extends BasePlugin {
}
allAggregationPaths.push(currentPath);
this.core.service.functions[aggregationFuncName]._handlers = handlers;
this.core.service.functions[aggregationFuncName]._allAggred = allAggred;
this.core.service.functions[aggregationFuncName].events = [
{ http: { method: 'get', path: currentPath } },
];
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-package/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function commonPrefix(arr: string[]): string {
prefix = commonPrefixUtil(prefix, arr[i].replace(/([^\/])$/, '$1/'));
}
if (!prefix || prefix === '/') {
return prefix;
return '';
}
const result = prefix.replace(/\/[^\/]*$/ig, '') || '/';
if (result && !/^\//.test(result)) {
Expand Down
14 changes: 14 additions & 0 deletions packages/faas-cli-plugin-package/test/commonPrefix.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { commonPrefix } from '../src/utils';
import * as assert from 'assert';
describe('/test/commonPrefix.test.ts', () => {
describe('commonPrefix', () => {
it('/', async () => {
const prefix = commonPrefix(['/api/index', '/api/api2', '/']);
assert(prefix === '');
});
it('/*', async () => {
const prefix = commonPrefix(['/api/index', '/api/api2', '//*']);
assert(prefix === '');
});
});
});
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-package/test/package.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandHookCore, loadSpec } from '@midwayjs/fcli-command-core';
import { PackagePlugin } from '../src/index';
import { join, resolve } from 'path';
import { resolve, join } from 'path';
import { existsSync, remove } from 'fs-extra';
import * as assert from 'assert';

Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli-plugin-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"typings": "dist/index.d.ts",
"dependencies": {
"@midwayjs/fcli-command-core": "^0.2.22",
"midway-bin": "1"
"midway-bin": "beta"
},
"files": [
"src",
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"f": "bin/fun.js"
},
"devDependencies": {
"midway-bin": "1"
"midway-bin": "beta"
},
"files": [
"bin",
Expand Down
4 changes: 3 additions & 1 deletion packages/faas-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export class CLI extends BaseCLI {

if (this.argv.v || this.argv.version) {
this.displayVersion();
} else { // 默认没有command的时候展示帮助
this.argv.h = true;
}
}

Expand All @@ -49,7 +51,7 @@ export class CLI extends BaseCLI {

async checkProvider() {
// ignore f -v / f -h / f create
if (!this.commands.length || this.commands[0] === 'create' || this.argv.h) {
if (!this.commands.length || this.argv.h || this.commands[0] === 'create') {
return;
}
if (!this.spec.provider) {
Expand Down
2 changes: 1 addition & 1 deletion packages/faas-util-ts-compile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"typings": "dist/index.d.ts",
"dependencies": {
"fs-extra": "^8.1.0",
"midway-bin": "1"
"midway-bin": "beta"
},
"scripts": {
"build": "npm run lint && midway-bin build -c",
Expand Down
113 changes: 70 additions & 43 deletions packages/faas-util-ts-compile/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
import { join, relative } from 'path';
import { remove, writeJSON } from 'fs-extra';
import { join, relative, resolve } from 'path';
import { readFileSync, existsSync } from 'fs-extra';
import { BuildCommand } from 'midway-bin';
import { combineTsConfig } from './utils';

export const tsIntegrationProjectCompile = async (baseDir, options: {
sourceDir: string;
buildRoot: string;
tsCodeRoot: string;
incremental: boolean;
tsConfig?: any; // 临时的ts配置
clean: boolean;
}) => {
const tsFaaSConfigFilename = 'tsconfig_integration_faas.json';
// 生成一个临时 tsconfig
const tempConfigFilePath = join(baseDir, tsFaaSConfigFilename);
await remove(tempConfigFilePath);
// 重新写一个新的
await writeJSON(tempConfigFilePath, {
compileOnSave: true,
compilerOptions: {
incremental: !!options.incremental,
target: 'ES2018',
module: 'commonjs',
moduleResolution: 'node',
experimentalDecorators: true,
emitDecoratorMetadata: true,
inlineSourceMap: true,
noImplicitThis: true,
noUnusedLocals: true,
stripInternal: true,
pretty: true,
declaration: true,
jsx: 'react',
outDir: relative(
baseDir,
join(options.buildRoot, 'dist')
),
},
include: [
`${relative(
baseDir,
options.tsCodeRoot
)}/**/*`,
],
exclude: ['dist', 'node_modules', 'test'],
});
await tsCompile(baseDir, {
tsConfigName: tsFaaSConfigFilename,
source: options.sourceDir,
const tsConfig = await tsCompile(baseDir, {
tsConfig: options.tsConfig,
clean: options.clean,
incremental: options.incremental,
innerTsConfig: {
compileOnSave: true,
compilerOptions: {
incremental: !!options.incremental,
target: 'ES2018',
module: 'commonjs',
moduleResolution: 'node',
experimentalDecorators: true,
emitDecoratorMetadata: true,
inlineSourceMap: true,
noImplicitThis: true,
noUnusedLocals: true,
stripInternal: true,
pretty: true,
declaration: true,
jsx: 'react',
outDir: relative(
baseDir,
join(options.buildRoot, 'dist')
),
},
include: [
`${relative(
baseDir,
options.tsCodeRoot
)}/**/*`
],
exclude: ['dist', 'node_modules', 'test'],
}
});
return tsConfig;
};

/**
Expand All @@ -59,17 +56,47 @@ export const tsIntegrationProjectCompile = async (baseDir, options: {
* @param options.clean 是否在构建前清理
*/
export const tsCompile = async (baseDir: string, options: {
source?: string;
tsConfigName?: string;
clean?: boolean;
innerTsConfig?: any;
tsConfig?: any; // extra tsconfig
incremental?: boolean;
} = {}) => {
const builder = new BuildCommand();
let tsJson = null;
if (options.tsConfigName) {
try {
tsJson = JSON.parse(readFileSync(resolve(baseDir, options.tsConfigName)).toString());
} catch (e) {}
}
const tsConfig = combineTsConfig({}, options.innerTsConfig, options.tsConfig, tsJson);

if (tsConfig.compilerOptions) {
if (tsConfig.compilerOptions.inlineSourceMap) {
tsConfig.compilerOptions.sourceMap = false;
}
if (options.incremental === true || options.incremental === false) {
tsConfig.compilerOptions.incremental = options.incremental;
}
if (tsConfig.compilerOptions.incremental) {
let tsBuildInfoFile = '';
if (tsConfig.compilerOptions.outDir && existsSync(tsConfig.compilerOptions.outDir)) {
tsBuildInfoFile = resolve(tsConfig.compilerOptions.outDir, '.tsbuildinfo');
} else {
const tmpDir = ['build', 'dist'].find(dirName => existsSync(resolve(baseDir, dirName)));
tsBuildInfoFile = resolve(tmpDir || baseDir, '.tsbuildinfo');
}
tsConfig.compilerOptions.tsBuildInfoFile = tsBuildInfoFile;
}
}

await builder.run({
cwd: baseDir,
argv: {
clean: typeof options.clean === 'undefined' ? true : options.clean,
project: options.tsConfigName || 'tsconfig.json',
srcDir: options.source || 'src',
tsConfig
},
});

return tsConfig;
};
23 changes: 23 additions & 0 deletions packages/faas-util-ts-compile/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const getType = (v: any) => (({}).toString.call(v).slice(8, -1).toLowerCase());
export const combineTsConfig = (config, ...tsConfig: any[]) => {
const combined = config || {};
for (const config of tsConfig || []) {
for (const key in config || {}) {
const value = config[key];
const valueType = getType(value);
if (valueType === 'object') {
combined[key] = combineTsConfig({}, combined[key], value);
} else if (valueType === 'array') {
if (!combined[key]) {
combined[key] = [];
}
combined[key].push(...value);
} else {
if (value || value === false) {
combined[key] = value;
}
}
}
}
return combined;
};
2 changes: 1 addition & 1 deletion packages/faas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"src"
],
"devDependencies": {
"midway-bin": "1"
"midway-bin": "beta"
},
"license": "MIT",
"scripts": {
Expand Down

0 comments on commit 9149d2a

Please sign in to comment.