Skip to content

Commit

Permalink
feat: http-mock-gen and utils package add "type": "module" as default
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Dec 6, 2023
1 parent 18dc13f commit e616e6f
Show file tree
Hide file tree
Showing 25 changed files with 97 additions and 27 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "git",
"url": "git+https://github.com/liangskyli/mock.git"
},
"type": "module",
"scripts": {
"build": "lerna run build",
"test": "lerna run test",
Expand All @@ -27,7 +28,7 @@
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"prettier --write",
"node script/lint"
"node script/lint.cjs"
]
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions packages/http-mock-gen/bin/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node
const { commandCodeGenCli } = require('../lib/index.cjs');
const { version } = require('../package.json');

commandCodeGenCli(version);
5 changes: 4 additions & 1 deletion packages/http-mock-gen/bin/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env node
const { commandCodeGenCli } = require('../lib/index.cjs');
import { createRequire } from 'node:module';
import { commandCodeGenCli } from '../lib/index.js';

const require = createRequire(import.meta.url);
const { version } = require('../package.json');

commandCodeGenCli(version);
17 changes: 13 additions & 4 deletions packages/http-mock-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@
},
"license": "MIT",
"author": "liangsky <[email protected]>",
"main": "./lib/index.cjs.js",
"module": "./lib/index.esm.js",
"type": "module",
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"types": "./lib/index.d.ts",
"typings": "./lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.cjs"
}
},
"bin": {
"http-mock-gen": "bin/index.js"
"http-mock-gen": "bin/index.js",
"http-mock-gen-cjs": "bin/index.cjs"
},
"files": [
"lib",
Expand All @@ -35,7 +44,7 @@
"scripts": {
"test": "vitest run",
"coverage": "vitest run --coverage",
"build": "rollup --bundleConfigAsCjs --config=./rollup.config.js",
"build": "rollup --config=./rollup.config.js",
"update:deps": "pnpm update --interactive --latest"
},
"dependencies": {
Expand Down
7 changes: 5 additions & 2 deletions packages/http-mock-gen/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createRequire } from 'node:module';
import copy from 'rollup-plugin-copy';
import { getConfig } from '../rollup.base.config';
import packageJSON from './package.json';
import { getConfig } from '../rollup.base.config.js';

const require = createRequire(import.meta.url);
const packageJSON = require('./package.json');

const config = getConfig(packageJSON);

Expand Down
5 changes: 4 additions & 1 deletion packages/http-mock-gen/src/gen/file/gen-custom-data.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { IPrettierOptions } from '@liangskyli/utils';
import { winPath } from '@liangskyli/utils';
import fs from 'fs-extra';
import path from 'path';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { getMethodData, packageName, writePrettierFile } from '../../utils';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export type IGenCustomDataOpts = {
mockData: any;
genCustomDataPath: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IPrettierOptions } from '@liangskyli/utils';
import { getRelativePath, winPath } from '@liangskyli/utils';
import fs from 'fs-extra';
import path from 'path';
import path from 'node:path';
import {
fileTip,
getMethodData,
Expand Down
2 changes: 1 addition & 1 deletion packages/http-mock-gen/src/gen/file/gen-mock-data-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { IPrettierOptions } from '@liangskyli/utils';
import { copyOptions } from '@liangskyli/utils';
import type { JSONSchemaFakerOptions, Schema } from 'json-schema-faker';
import { JSONSchemaFaker } from 'json-schema-faker';
import path from 'path';
import path from 'node:path';
import { writePrettierFile } from '../../utils';

type IGenMockDataJsonOpts = {
Expand Down
7 changes: 5 additions & 2 deletions packages/http-mock-gen/src/gen/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IGenTsDataOpts } from '@liangskyli/openapi-gen-ts';
import genTsData from '@liangskyli/openapi-gen-ts';
import {
colors,
copyOptions,
Expand All @@ -8,9 +7,13 @@ import {
} from '@liangskyli/utils';
import fs from 'fs-extra';
import type { JSONSchemaFakerOptions } from 'json-schema-faker';
import path from 'path';
import { createRequire } from 'node:module';
import path from 'node:path';
import generatorMockFile from './file';

const require = createRequire(import.meta.url);
const genTsData = require('@liangskyli/openapi-gen-ts').default;

export type IGenMockDataBaseOpts = {
mockDir?: string;
mockPathPrefix?: string;
Expand Down
17 changes: 16 additions & 1 deletion packages/http-mock-gen/test/cli/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`CLI > CLI test 1`] = `
exports[`CLI > CLI test cjs 1`] = `
"use configFile path: ./test/cli/mock.config.cli.ts
Clean mock dir: test/all-gen-dirs/gen-mock-cli/mock
Clean schema-api dir: test/all-gen-dirs/gen-mock-cli/mock/schema-api
Generate schema-api/ts-schema.ts success
Generate schema-api/schema.json success
Generate schema-api/request.ts file success
Generate schema-api/interface-api.ts file success
Generate schema-api/request-api.ts file success
Generate mock/mock-data.json success
Generate mock/custom-data/index.ts file success
Generate mock/custom-data/template-data.ts file success
Generate mock/interface-mock-data.ts file success"
`;

exports[`CLI > CLI test esm 1`] = `
"use configFile path: ./test/cli/mock.config.cli.ts
Clean mock dir: test/all-gen-dirs/gen-mock-cli/mock
Clean schema-api dir: test/all-gen-dirs/gen-mock-cli/mock/schema-api
Expand Down
18 changes: 16 additions & 2 deletions packages/http-mock-gen/test/cli/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { winPath } from '@liangskyli/utils';
import { execa } from 'execa';
import fs from 'fs-extra';
import path from 'node:path';
import { URL } from 'node:url';
import path from 'path';
import { describe, expect, test } from 'vitest';

const cwd = new URL('../../', import.meta.url);
const cmd = 'node';
describe('CLI', () => {
test('CLI test', async () => {
test('CLI test esm', async () => {
await fs.remove(
path.join(__dirname, '../all-gen-dirs/gen-mock-cli/mock/custom-data'),
);
Expand All @@ -21,4 +21,18 @@ describe('CLI', () => {
);
await expect(winPath(stdout)).toMatchSnapshot();
});

test('CLI test cjs', async () => {
await fs.remove(
path.join(__dirname, '../all-gen-dirs/gen-mock-cli/mock/custom-data'),
);
const { stdout } = await execa(
cmd,
['./bin/index.cjs', '-c', './test/cli/mock.config.cli.ts'],
{
cwd,
},
);
await expect(winPath(stdout)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { winPath } from '@liangskyli/utils';
import fs from 'fs-extra';
import path from 'path';
import path from 'node:path';
import { describe, expect, test, vi } from 'vitest';
import { GenCustomData } from '../../../src/gen/file/gen-custom-data';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra';
import path from 'path';
import path from 'node:path';
import { describe, expect, test, vi } from 'vitest';
import { GenInterfaceMockData } from '../../../src/gen/file/gen-interface-mock-data';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra';
import path from 'path';
import path from 'node:path';
import { describe, expect, test, vi } from 'vitest';
import { GenMockDataJson } from '../../../src/gen/file/gen-mock-data-json';

Expand Down
2 changes: 1 addition & 1 deletion packages/http-mock-gen/test/gen/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra';
import path from 'path';
import path from 'node:path';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import genMockData from '../../src/gen';

Expand Down
14 changes: 11 additions & 3 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@
},
"license": "MIT",
"author": "liangsky <[email protected]>",
"main": "./lib/index.cjs.js",
"module": "./lib/index.esm.js",
"type": "module",
"main": "./lib/index.cjs",
"module": "./lib/index.js",
"types": "./lib/index.d.ts",
"typings": "./lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.cjs"
}
},
"files": [
"lib",
"CHANGELOG.md",
Expand All @@ -27,7 +35,7 @@
"scripts": {
"test": "vitest run",
"coverage": "vitest run --coverage",
"build": "rollup --bundleConfigAsCjs --config=./rollup.config.js",
"build": "rollup --config=./rollup.config.js",
"update:deps": "pnpm update --interactive --latest"
},
"dependencies": {
Expand Down
7 changes: 5 additions & 2 deletions packages/utils/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getConfig } from '../rollup.base.config';
import packageJSON from './package.json';
import { createRequire } from 'node:module';
import { getConfig } from '../rollup.base.config.js';

const require = createRequire(import.meta.url);
const packageJSON = require('./package.json');

export default [getConfig(packageJSON)];
3 changes: 3 additions & 0 deletions packages/utils/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createRequire } from 'node:module';
import { register } from './register';

const require = createRequire(import.meta.url);

const getConfig = (configFile: string) => {
let config: any = null;
const { unregister } = register({ key: 'utils-getConfig' });
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra';
import path from 'path';
import path from 'node:path';
import prettier from 'prettier';

export const getAbsolutePath = (pathName: string) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/test/tools.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra';
import path from 'path';
import path from 'node:path';
import { describe, expect, test } from 'vitest';
import {
copyOptions,
Expand Down
File renamed without changes.

0 comments on commit e616e6f

Please sign in to comment.