Skip to content

Commit

Permalink
feat: prettierOptions type use @liangskyli/utils IPrettierOptions type
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Jan 19, 2023
1 parent 1e7f6d0 commit c6a7295
Show file tree
Hide file tree
Showing 10 changed files with 2,524 additions and 6,862 deletions.
1 change: 1 addition & 0 deletions packages/grpc-mock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"tslib": "^2.4.1"
},
"devDependencies": {
"@liangskyli/grpc-mock": "workspace:^1.5.0",
"@types/cross-spawn": "^6.0.2",
"@types/express": "^4.17.14",
"@types/mockjs": "^1.0.7",
Expand Down
26 changes: 19 additions & 7 deletions packages/grpc-mock/src/grpc/gen-grpc-obj.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { fileTip, getImportPath, packageName, tslintDisable } from './utils';
import type { Options } from '@grpc/proto-loader';
import type { IPrettierOptions } from '@liangskyli/utils';
import { colors, getAbsolutePath, prettierData } from '@liangskyli/utils';
import * as fs from 'fs-extra';
import path from 'path';
import type { Options } from '@grpc/proto-loader';
import type prettier from 'prettier';
import { fileTip, getImportPath, packageName, tslintDisable } from './utils';

type GenGrpcObjOptions = {
genMockPath: string;
rootPath: string;
grpcNpmName: string;
loaderOptions: Options;
configFilePath?: string;
prettierOptions?: prettier.Options;
prettierOptions?: IPrettierOptions;
};

const genGrpcObj = async (opt: GenGrpcObjOptions) => {
const { grpcNpmName, genMockPath, rootPath, configFilePath, prettierOptions } = opt;
const {
grpcNpmName,
genMockPath,
rootPath,
configFilePath,
prettierOptions,
} = opt;
const grpcObjPath = getAbsolutePath(path.join(genMockPath, 'grpc-obj.ts'));

const fileContent = [
Expand All @@ -29,7 +35,10 @@ const genGrpcObj = async (opt: GenGrpcObjOptions) => {
`const root = require('${getImportPath(grpcObjPath, rootPath)}');\n`,
'let config: any;',
configFilePath
? `if (fs.existsSync(require.resolve('${getImportPath(grpcObjPath, configFilePath)}'))) {
? `if (fs.existsSync(require.resolve('${getImportPath(
grpcObjPath,
configFilePath,
)}'))) {
config = require('${getImportPath(grpcObjPath, configFilePath)}').default;
}`
: '',
Expand All @@ -43,7 +52,10 @@ const genGrpcObj = async (opt: GenGrpcObjOptions) => {
'export default grpcObjectGroup;',
].join('\n');

fs.writeFileSync(grpcObjPath, await prettierData(fileContent, prettierOptions));
fs.writeFileSync(
grpcObjPath,
await prettierData(fileContent, prettierOptions),
);
console.info(colors.green(`Generate grpc-obj.ts success in ${genMockPath}`));
};

Expand Down
64 changes: 45 additions & 19 deletions packages/grpc-mock/src/grpc/gen-mock-data.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import protobufjs from 'protobufjs';
import type { IInspectNamespace } from './pbjs';
import { genImplementationData, inspectNamespace } from './pbjs';
import { fileTip, firstUpperCaseOfWord, firstWordNeedLetter, packageName } from './utils';
import type { Options } from '@grpc/proto-loader';
import type { IPrettierOptions } from '@liangskyli/utils';
import {
colors,
copyOptions,
getAbsolutePath,
prettierData,
copyOptions,
winPath,
removeFilesSync,
colors,
winPath,
} from '@liangskyli/utils';
import * as fs from 'fs-extra';
import path from 'path';
import protobufjs from 'protobufjs';
import type { ProtoConfig } from './gen-proto-json';
import genProtoJson from './gen-proto-json';
import type { Options } from '@grpc/proto-loader';
import type prettier from 'prettier';
import type { IInspectNamespace } from './pbjs';
import { genImplementationData, inspectNamespace } from './pbjs';
import {
fileTip,
firstUpperCaseOfWord,
firstWordNeedLetter,
packageName,
} from './utils';

export type GenMockDataOptions = {
grpcMockDir?: string;
grpcMockFolderName?: string;
port?: number;
rootPath: ProtoConfig | string;
rootPathServerNameMap?: Record<string, string>;
prettierOptions?: prettier.Options;
prettierOptions?: IPrettierOptions;
};

const genDefaultCustomData = async (
genCustomDataPath: string,
prettierOptions?: prettier.Options,
prettierOptions?: IPrettierOptions,
) => {
if (!fs.pathExistsSync(path.join(genCustomDataPath, 'index.ts'))) {
fs.ensureDirSync(genCustomDataPath);
Expand Down Expand Up @@ -109,13 +114,16 @@ const genMockData = async (
const rootObject = require(rootPath);
await Promise.all(
Object.keys(rootObject).map(async (spaceServerName) => {
const serverName: string = rootPathServerNameMap?.[spaceServerName] ?? spaceServerName;
const serverName: string =
rootPathServerNameMap?.[spaceServerName] ?? spaceServerName;
const root = protobufjs.Root.fromJSON(rootObject[spaceServerName]);
const result: IInspectNamespace = inspectNamespace(root);
const { services, methods } = result!;
const serviceMockContent = [];
serviceMockContent.push(fileTip);
serviceMockContent.push(`import type { IMockService } from '${packageName}';`);
serviceMockContent.push(
`import type { IMockService } from '${packageName}';`,
);
const protoItem: string[] = [];
const uniqueServiceCodeNameList: string[] = [];
const longsTypeToString = loaderOptions.longs === String;
Expand Down Expand Up @@ -151,10 +159,18 @@ const genMockData = async (
export default ${serviceCodeName};
`;
fs.ensureDirSync(path.join(genProtoPath, serverName, protoName));
const filePath = path.join(genProtoPath, serverName, protoName, `${serviceCodeName}.ts`);
const filePath = path.join(
genProtoPath,
serverName,
protoName,
`${serviceCodeName}.ts`,
);
fs.writeFileSync(
filePath,
await prettierData(protoServiceContent, copyOptions(prettierOptions)),
await prettierData(
protoServiceContent,
copyOptions(prettierOptions),
),
);

serviceMockContent.push(
Expand All @@ -163,7 +179,9 @@ const genMockData = async (
protoItem.push(`{ ...${uniqueServiceCodeName} },`);
}),
);
const spaceServerNameMock = `${firstUpperCaseOfWord(spaceServerName)}Mock`;
const spaceServerNameMock = `${firstUpperCaseOfWord(
spaceServerName,
)}Mock`;
serviceMockContent.push(`
const ${spaceServerNameMock}: IMockService = {
serviceName: '${serverName}',
Expand All @@ -184,10 +202,15 @@ export default ${spaceServerNameMock};
const filePath = path.join(genServerPath, `${spaceServerNameMock}.ts`);
fs.writeFileSync(
filePath,
await prettierData(serviceMockContent.join('\n'), copyOptions(prettierOptions)),
await prettierData(
serviceMockContent.join('\n'),
copyOptions(prettierOptions),
),
);
spaceServerNameMockList.push(spaceServerNameMock);
indexContent.push(`import ${spaceServerNameMock} from './server/${spaceServerNameMock}';`);
indexContent.push(
`import ${spaceServerNameMock} from './server/${spaceServerNameMock}';`,
);
}),
);
// index.ts
Expand All @@ -204,7 +227,10 @@ export default ${spaceServerNameMock};
const fileConfigPath = path.join(genMockPath, 'grpc-service.mock.config.js');
fs.writeFileSync(
fileConfigPath,
await prettierData(grpcServiceMockConfigList.join('\n'), copyOptions(prettierOptions)),
await prettierData(
grpcServiceMockConfigList.join('\n'),
copyOptions(prettierOptions),
),
);
console.info(colors.green(`Generate mock data success in ${genMockPath}`));

Expand Down
4 changes: 2 additions & 2 deletions packages/grpc-mock/src/grpc/gen-proto-json.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Options } from '@grpc/proto-loader';
import type { IPrettierOptions } from '@liangskyli/utils';
import { colors, getAbsolutePath, prettierData } from '@liangskyli/utils';
import * as fs from 'fs-extra';
import path from 'path';
import type prettier from 'prettier';
import protobufjs from 'protobufjs';

export type ProtoConfig = {
Expand All @@ -17,7 +17,7 @@ export type ProtoConfig = {
type GenProtoOptions = ProtoConfig & {
genMockPath: string;
loaderOptions: Options;
prettierOptions?: prettier.Options;
prettierOptions?: IPrettierOptions;
};

const getProtoFiles = (absoluteDir: string): string[] => {
Expand Down
9 changes: 6 additions & 3 deletions packages/grpc-mock/src/grpc/gen-tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { IPrettierOptions } from '@liangskyli/utils';
import { prettierData } from '@liangskyli/utils';
import * as fs from 'fs-extra';
import path from 'path';
import type prettier from 'prettier';
import { prettierData } from '@liangskyli/utils';

const genTsConfig = async (genMockPath: string, prettierOptions?: prettier.Options) => {
const genTsConfig = async (
genMockPath: string,
prettierOptions?: IPrettierOptions,
) => {
const genMockPathArray = path.join(genMockPath).split(path.sep);
// 生成tsconfig.mock.json文件
const tsconfigMockConfigPath = path.join(genMockPath, 'tsconfig.mock.json');
Expand Down
1 change: 1 addition & 0 deletions packages/http-mock-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"ts-node": "^10.9.1"
},
"devDependencies": {
"@liangskyli/http-mock-gen": "workspace:^1.5.0",
"@types/cross-spawn": "^6.0.2",
"@types/express": "^4.17.14"
},
Expand Down
20 changes: 13 additions & 7 deletions packages/http-mock-gen/src/gen/gen-mock-data-file.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Definition } from '@liangskyli/openapi-gen-ts';
import type { IPrettierOptions } from '@liangskyli/utils';
import { colors, prettierData } from '@liangskyli/utils';
import fs from 'fs-extra';
import type { JSONSchemaFakerOptions } from 'json-schema-faker';
import jsf from 'json-schema-faker';
import fs from 'fs-extra';
import path from 'path';
import type prettier from 'prettier';
import { colors, prettierData } from '@liangskyli/utils';
import type { Definition } from '@liangskyli/openapi-gen-ts';

type IOpts = {
genMockAbsolutePath: string;
schemaDefinition: Definition;
prettierOptions?: prettier.Options;
prettierOptions?: IPrettierOptions;
jsonSchemaFakerOptions?: JSONSchemaFakerOptions;
mockDataReplace?: (this: any, key: string, value: any) => any;
};
Expand All @@ -23,7 +23,10 @@ const genMockDataFile = async (opts: IOpts) => {
prettierOptions = Object.assign(prettierOptions, { parser: 'json' });

if (jsonSchemaFakerOptions === undefined) {
jsonSchemaFakerOptions = { alwaysFakeOptionals: true, fillProperties: false };
jsonSchemaFakerOptions = {
alwaysFakeOptionals: true,
fillProperties: false,
};
}
jsonSchemaFakerOptions = Object.assign(jsonSchemaFakerOptions, {
alwaysFakeOptionals: true,
Expand All @@ -34,7 +37,10 @@ const genMockDataFile = async (opts: IOpts) => {
const mockData = jsf.generate(schemaDefinition as any);
const mockDataString = JSON.stringify(mockData, mockDataReplace, 2);
const mockDataAbsolutePath = path.join(genMockAbsolutePath, 'mock-data.json');
fs.writeFileSync(mockDataAbsolutePath, await prettierData(mockDataString, prettierOptions));
fs.writeFileSync(
mockDataAbsolutePath,
await prettierData(mockDataString, prettierOptions),
);

console.info(colors.green('Generate mock/mock-data.json success'));

Expand Down
6 changes: 3 additions & 3 deletions packages/http-mock-gen/src/gen/gen-mock-interface-file.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { IPrettierOptions } from '@liangskyli/utils';
import { colors, copyOptions, prettierData, winPath } from '@liangskyli/utils';
import fs from 'fs-extra';
import path from 'path';
import type prettier from 'prettier';
import { fileTip, packageName } from '../utils';

type IOpts = {
interfaceApiRelativePath: string;
mockDataAbsolutePath: string;
genMockAbsolutePath: string;
prettierOptions?: prettier.Options;
prettierOptions?: IPrettierOptions;
mockPathPrefix?: string;
};

type IDefaultOpts = {
mockData: any;
genCustomDataPath: string;
interfaceApiRelativePath: string;
prettierOptions?: prettier.Options;
prettierOptions?: IPrettierOptions;
};

const getMediaTypeData = (content: any) => {
Expand Down
14 changes: 8 additions & 6 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import address from 'address';
import colors from 'colors';
import createDebug from 'debug';
import lodash from 'lodash';
import signale from 'signale';
import createDebug from 'debug';
import colors from 'colors';
import type { prettierData } from './tools';

export { default as getConfig } from './config';
export { default as parseRequireDeps } from './parse-require-deps/parse-require-deps';
export {
copyOptions,
getAbsolutePath,
getRelativePath,
prettierData,
copyOptions,
winPath,
removeFilesSync,
winPath,
} from './tools';
export { default as getConfig } from './config';
export { address, colors, lodash, signale, createDebug };
export { default as parseRequireDeps } from './parse-require-deps/parse-require-deps';
export type IPrettierOptions = Parameters<typeof prettierData>[1];
Loading

0 comments on commit c6a7295

Please sign in to comment.