create-stylelint 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -1
  2. package/src/index.mjs +15 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-stylelint",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "The npm init script for Stylelint.",
5
5
  "keywords": [
6
6
  "stylelint",
@@ -49,6 +49,7 @@
49
49
  ]
50
50
  },
51
51
  "dependencies": {
52
+ "cosmiconfig": "^8.1.0",
52
53
  "execa": "^7.0.0",
53
54
  "ora": "^6.1.2",
54
55
  "picocolors": "^1.0.0",
package/src/index.mjs CHANGED
@@ -1,31 +1,21 @@
1
1
  /* eslint-disable no-process-exit */
2
2
  /* eslint no-console: 'off' */
3
3
 
4
+ import * as fs from 'node:fs';
5
+ import * as path from 'node:path';
6
+ import { cosmiconfigSync } from 'cosmiconfig';
4
7
  import detectPackageManager from 'which-pm-runs';
5
8
  import { execa } from 'execa';
6
- import fs from 'fs';
7
9
  import ora from 'ora';
8
10
  import picocolors from 'picocolors';
9
11
  import stripIndent from 'strip-indent';
10
12
 
11
13
  const DEFAULT_CONFIG_FILE = '.stylelintrc.json';
12
14
 
13
- const STYLELINT_CONFIG_PATHS = new Set([
14
- '.stylelintrc',
15
- '.stylelintrc.cjs',
16
- '.stylelintrc.json',
17
- '.stylelintrc.yaml',
18
- '.stylelintrc.yml',
19
- '.stylelintrc.js',
20
- 'stylelint.config.js',
21
- 'stylelint.config.cjs',
22
- ]);
15
+ function getExistingConfigInDirectory() {
16
+ const explorer = cosmiconfigSync('stylelint');
23
17
 
24
- /**
25
- * @param {fs.PathLike} dir
26
- */
27
- function getExistingConfigsInDirectory(dir) {
28
- return fs.readdirSync(dir).filter((file) => STYLELINT_CONFIG_PATHS.has(file));
18
+ return explorer.search();
29
19
  }
30
20
 
31
21
  /**
@@ -48,14 +38,16 @@ function getInstallCommand(pkgManager) {
48
38
  */
49
39
  function createConfig(cwd, pkgManager) {
50
40
  const spinner = ora('Creating config...').start();
51
- const existingConfigs = getExistingConfigsInDirectory(cwd);
41
+ const existingConfig = getExistingConfigInDirectory();
52
42
 
53
- if (existingConfigs.length > 0) {
54
- spinner.fail(
55
- `Failed to create config:\nThe ${existingConfigs.join(
56
- ', ',
57
- )} config(s) already exist. Remove them and then try again.`,
58
- );
43
+ if (existingConfig !== null) {
44
+ const basename = path.basename(existingConfig.filepath);
45
+ const failureMessage =
46
+ basename === 'package.json'
47
+ ? 'The "stylelint" config in "package.json" already exists.'
48
+ : `The "${basename}" config already exists.`;
49
+
50
+ spinner.fail(`Failed to create config:\n${failureMessage} Remove it and then try again.`);
59
51
  process.exit(1);
60
52
  }
61
53