create-stylelint 0.1.1

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.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # create-stylelint
2
+
3
+ The [`npm init`](https://docs.npmjs.com/cli/v9/commands/npm-init) script for [Stylelint](https://github.com/stylelint/stylelint).
4
+
5
+ ## Usage
6
+
7
+ Run `npm init stylelint`
8
+
9
+ ## Contributing
10
+
11
+ - `npm install`
12
+ - `node create-stylelint.mjs` to run locally
13
+ - `npm run watch` to test with [Vitest](https://vitest.dev/)
14
+
15
+ ## Acknowledgments
16
+
17
+ Inspired by [create-astro](https://github.com/withastro/astro/blob/main/packages/create-astro).
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-process-exit */
3
+ /* eslint-disable no-console */
4
+ 'use strict';
5
+
6
+ const currentVersion = process.versions.node;
7
+
8
+ import { fileURLToPath } from 'node:url';
9
+ import fs from 'node:fs';
10
+ import path from 'node:path';
11
+ import semver from 'semver';
12
+
13
+ const dirname = path.dirname(fileURLToPath(import.meta.url));
14
+ const pkg = JSON.parse(fs.readFileSync(path.join(dirname, 'package.json').toString()));
15
+
16
+ if (!semver.satisfies(currentVersion, pkg.engines.node)) {
17
+ console.error(`Unsupported Node.js version (v${currentVersion})`);
18
+ console.error(`Install a Node.js version within "${pkg.engines.node}" and then try again.`);
19
+ process.exit(1);
20
+ }
21
+
22
+ // eslint-disable-next-line node/no-unsupported-features/es-syntax
23
+ import('./src/index.mjs').then(({ main }) => main());
package/package.json ADDED
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "create-stylelint",
3
+ "version": "0.1.1",
4
+ "description": "The npm init script for Stylelint.",
5
+ "keywords": [
6
+ "stylelint",
7
+ "npm init"
8
+ ],
9
+ "repository": "stylelint/create-stylelint",
10
+ "funding": {
11
+ "type": "opencollective",
12
+ "url": "https://opencollective.com/stylelint"
13
+ },
14
+ "license": "MIT",
15
+ "author": "stylelint",
16
+ "main": "create-stylelint.mjs",
17
+ "bin": {
18
+ "create-stylelint": "./create-stylelint.mjs"
19
+ },
20
+ "files": [
21
+ "src"
22
+ ],
23
+ "scripts": {
24
+ "format": "prettier . --write --cache",
25
+ "lint": "npm-run-all --parallel --continue-on-error lint:*",
26
+ "lint:formatting": "prettier . --check --cache",
27
+ "lint:js": "eslint . --cache --max-warnings=0 --ext .js,.mjs",
28
+ "lint:md": "remark . --quiet --frail",
29
+ "lint:packagejson": "npmPkgJsonLint .",
30
+ "lint:types": "tsc",
31
+ "pretest": "npm run lint",
32
+ "test": "vitest run",
33
+ "watch": "vitest watch",
34
+ "release": "np"
35
+ },
36
+ "prettier": "@stylelint/prettier-config",
37
+ "eslintConfig": {
38
+ "settings": {
39
+ "jest": {
40
+ "version": "latest"
41
+ }
42
+ },
43
+ "extends": [
44
+ "stylelint"
45
+ ],
46
+ "root": true
47
+ },
48
+ "npmpackagejsonlint": {
49
+ "extends": "@stylelint/npm-package-json-lint-config"
50
+ },
51
+ "remarkConfig": {
52
+ "plugins": [
53
+ "@stylelint/remark-preset"
54
+ ]
55
+ },
56
+ "dependencies": {
57
+ "execa": "^6.1.0",
58
+ "ora": "^6.1.2",
59
+ "picocolors": "^1.0.0",
60
+ "semver": "^7.3.8",
61
+ "strip-indent": "^4.0.0",
62
+ "which-pm-runs": "^1.1.0"
63
+ },
64
+ "devDependencies": {
65
+ "@stylelint/npm-package-json-lint-config": "^3.0.0",
66
+ "@stylelint/prettier-config": "^2.0.0",
67
+ "@stylelint/remark-preset": "^4.0.0",
68
+ "@types/which-pm-runs": "^1.0.0",
69
+ "eslint": "^8.28.0",
70
+ "eslint-config-stylelint": "^17.1.0",
71
+ "np": "^7.6.3",
72
+ "npm-run-all": "^4.1.5",
73
+ "remark-cli": "^11.0.0",
74
+ "typescript": "^4.9.5",
75
+ "vitest": "^0.28.4"
76
+ },
77
+ "engines": {
78
+ "node": "^14.13.1 || >=16.0.0 || >=18.0.0"
79
+ }
80
+ }
package/src/index.mjs ADDED
@@ -0,0 +1,119 @@
1
+ /* eslint-disable no-process-exit */
2
+ /* eslint no-console: 'off' */
3
+
4
+ import detectPackageManager from 'which-pm-runs';
5
+ import { execa } from 'execa';
6
+ import fs from 'fs';
7
+ import ora from 'ora';
8
+ import picocolors from 'picocolors';
9
+ import stripIndent from 'strip-indent';
10
+
11
+ const DEFAULT_CONFIG_FILE = '.stylelintrc.json';
12
+
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
+ ]);
23
+
24
+ /**
25
+ * @param {fs.PathLike} dir
26
+ */
27
+ function getExistingConfigsInDirectory(dir) {
28
+ return fs.readdirSync(dir).filter((file) => STYLELINT_CONFIG_PATHS.has(file));
29
+ }
30
+
31
+ /**
32
+ * @param {fs.PathLike} dir
33
+ */
34
+ function directoryHasPackageJson(dir) {
35
+ return fs.readdirSync(dir).some((file) => file === 'package.json');
36
+ }
37
+
38
+ /**
39
+ * @param {string} pkgManager
40
+ */
41
+ function getInstallCommand(pkgManager) {
42
+ return pkgManager === 'npm' ? 'install' : 'add';
43
+ }
44
+
45
+ /**
46
+ * @param {string} cwd
47
+ * @param {string} pkgManager
48
+ */
49
+ function createConfig(cwd, pkgManager) {
50
+ const spinner = ora('Creating config...').start();
51
+ const existingConfigs = getExistingConfigsInDirectory(cwd);
52
+
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
+ );
59
+ process.exit(1);
60
+ }
61
+
62
+ if (!directoryHasPackageJson(cwd)) {
63
+ spinner.fail(
64
+ `Failed to create config:\npackage.json was not found. Run "${pkgManager} init" and then try again.`,
65
+ );
66
+ process.exit(1);
67
+ }
68
+
69
+ try {
70
+ fs.writeFileSync(DEFAULT_CONFIG_FILE, '{ "extends": ["stylelint-config-standard"] }');
71
+ } catch (error) {
72
+ spinner.fail(`Failed to create config:\n${error}`);
73
+ process.exit(1);
74
+ }
75
+
76
+ spinner.succeed(`Created ${DEFAULT_CONFIG_FILE}.`);
77
+ }
78
+
79
+ /**
80
+ * @param {string} cwd
81
+ * @param {string} pkgManager
82
+ */
83
+ async function installPackages(cwd, pkgManager) {
84
+ const spinner = ora('Installing packages...').start();
85
+
86
+ try {
87
+ await execa(
88
+ pkgManager,
89
+ [`${getInstallCommand(pkgManager)}`, '-D', 'stylelint', 'stylelint-config-standard'],
90
+ { cwd },
91
+ );
92
+ } catch (error) {
93
+ spinner.fail(`Failed to install packages:\n${error}`);
94
+ process.exit(1);
95
+ }
96
+
97
+ spinner.succeed('Installed packages.');
98
+ }
99
+
100
+ function showNextSteps() {
101
+ console.log(
102
+ stripIndent(`
103
+ ${picocolors.green(`You can now lint your CSS files using:
104
+ npx stylelint "**/*.css"`)}
105
+
106
+ ${picocolors.dim(`We recommend customizing Stylelint:
107
+ https://stylelint.io/user-guide/customize/`)}
108
+ `),
109
+ );
110
+ }
111
+
112
+ export async function main() {
113
+ const pkgManager = detectPackageManager()?.name ?? 'npm';
114
+ const cwd = './';
115
+
116
+ createConfig(cwd, pkgManager);
117
+ await installPackages(cwd, pkgManager);
118
+ showNextSteps();
119
+ }