Skip to content

Commit f3154e1

Browse files
committed
--wip-- [skip ci]
1 parent 7b8a80e commit f3154e1

File tree

8 files changed

+87
-7
lines changed

8 files changed

+87
-7
lines changed

lib/config-generator.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export class ConfigGenerator {
1515
this.cwd = options.cwd || process.cwd();
1616
this.answers = options.answers || {};
1717
this.result = {
18-
devDependencies: [],
19-
configPath: "",
18+
devDependencies: ["eslint"],
19+
configFilename: "eslint.config.js",
2020
configContent: ""
2121
};
2222
}
@@ -33,10 +33,13 @@ export class ConfigGenerator {
3333
}
3434

3535
calc() {
36-
const isModule = isPackageTypeModule();
36+
const isModule = isPackageTypeModule(this.cwd);
37+
console.log(this.cwd, isModule)
3738

38-
this.result.configPath = path.join(this.cwd, isModule ? "eslint.config.js" : "eslint.config.mjs");
39+
this.result.configFilename = isModule ? "eslint.config.js" : "eslint.config.mjs";
3940

41+
if (this.answers.purpose === "syntax") { }
42+
if(this.answers.purpose === "problem"){}
4043
let importContent = "";
4144
let exportContent = "";
4245

lib/utils/npm-utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ function checkPackageJson(startDir) {
157157

158158
/**
159159
* check if the package.type === "module"
160+
* @param {string} [cwd] The current working directory
160161
* @returns {boolean} return true if the package.type === "module"
161162
*/
162-
function isPackageTypeModule() {
163-
const pkgJSONPath = findPackageJson();
163+
function isPackageTypeModule(cwd) {
164+
const pkgJSONPath = findPackageJson(cwd);
164165

165166
if (pkgJSONPath) {
166167
const pkgJSONContents = JSON.parse(fs.readFileSync(pkgJSONPath, "utf8"));

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"release:generate:beta": "eslint-generate-prerelease beta",
3737
"release:generate:rc": "eslint-generate-prerelease rc",
3838
"release:publish": "eslint-publish-release",
39-
"test": "c8 mocha \"tests/init/**/*.js\""
39+
"test": "c8 mocha \"tests/init/**/*.js\"",
40+
"debug": "vitest snapshots"
4041
},
4142
"mocha": {
4243
"loader": "esmock",
@@ -68,6 +69,7 @@
6869
"mocha": "^9.1.3",
6970
"shelljs": "^0.8.4",
7071
"sinon": "^12.0.1",
72+
"vitest": "^1.1.1",
7173
"yorkie": "^2.0.0"
7274
},
7375
"engines": {

tests/__snapshots__/syntax/cjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"configContent": "import PluginReact from 'eslint-plugin-react';
3+
export default [PluginReact.configs.recommended,];",
4+
"configFilename": "eslint.config.mjs",
5+
"devDependencies": [
6+
"eslint",
7+
"eslint-plugin-react",
8+
],
9+
}

tests/__snapshots__/syntax/esm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"configContent": "import PluginReact from 'eslint-plugin-react';
3+
export default [PluginReact.configs.recommended,];",
4+
"configFilename": "eslint.config.js",
5+
"devDependencies": [
6+
"eslint",
7+
"eslint-plugin-react",
8+
],
9+
}

tests/config-snapshots.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @fileoverview snapshot tests for config-generator.js
3+
* run `npm run test:snapshots` to assert snapshots
4+
* run `npm run test:snapshots:update` to update snapshots - you need
5+
* to check the changes manually (see the diff) and make sure it's correct.
6+
7+
* @author 唯然<[email protected]>
8+
*/
9+
import { ConfigGenerator } from "../lib/config-generator.js";
10+
import { expect, describe, it, test } from 'vitest'
11+
import { fileURLToPath } from 'node:url';
12+
import { join } from 'path'
13+
14+
const __filename = fileURLToPath(import.meta.url);
15+
const esmProjectDir = join(__filename, '../fixtures/esm-project');
16+
const cjsProjectDir = join(__filename, '../fixtures/cjs-project');
17+
18+
const inputs = [
19+
{
20+
name: 'syntax',
21+
answers: {
22+
purpose: 'syntax',
23+
module: 'esm',
24+
framework: 'react',
25+
typescript: false,
26+
}
27+
}
28+
]
29+
describe('generate config for projects', () => {
30+
inputs.forEach((item) => {
31+
test('esm projects', () => {
32+
const generator = new ConfigGenerator({cwd: esmProjectDir, answers: item.answers});
33+
generator.calc();
34+
expect(generator.result).toMatchFileSnapshot(`./__snapshots__/${item.name}/esm`)
35+
})
36+
37+
test('cjs projects', () => {
38+
const generator = new ConfigGenerator({cwd: cjsProjectDir, answers: item.answers});
39+
generator.calc();
40+
expect(generator.result).toMatchFileSnapshot(`./__snapshots__/${item.name}/cjs`)
41+
})
42+
})
43+
})
44+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "esm",
3+
"private": true,
4+
"type": "commonjs",
5+
"version": "1.0.0"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "esm",
3+
"private": true,
4+
"type": "module",
5+
"version": "1.0.0"
6+
}

0 commit comments

Comments
 (0)