From 60ee94f58f085c9f85a73638501a1baac67507a7 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 15 May 2024 09:30:12 -0700 Subject: [PATCH] feat: add prettier support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Julian Møller Ellehauge --- .gitignore | 7 +- lib/config.js | 97 +++++++++++++++---- lib/content/eslintrc-js.hbs | 9 +- lib/content/gitignore.hbs | 3 - lib/content/index.js | 18 +++- lib/content/package-json.hbs | 14 ++- lib/content/prettier-js.hbs | 6 ++ lib/content/prettierignore.hbs | 3 + lib/util/parser.js | 2 +- lib/util/template.js | 1 + package.json | 8 +- .../test/apply/source-snapshots.js.test.cjs | 64 ++++++------ .../test/check/snapshots.js.test.cjs | 36 ++++--- test/apply/eslint.js | 24 ----- test/apply/lint.js | 61 ++++++++++++ workspace/test-workspace/.gitignore | 5 +- workspace/test-workspace/package.json | 7 +- 17 files changed, 253 insertions(+), 112 deletions(-) create mode 100644 lib/content/prettier-js.hbs create mode 100644 lib/content/prettierignore.hbs delete mode 100644 test/apply/eslint.js create mode 100644 test/apply/lint.js diff --git a/.gitignore b/.gitignore index e0bd2dd9..9d61ca63 100644 --- a/.gitignore +++ b/.gitignore @@ -2,17 +2,17 @@ # ignore everything in the root /* -# transient test directories -tap-testdir*/ -# keep these !**/.gitignore !/.commitlintrc.js !/.eslintrc.js !/.eslintrc.local.* +!/.git-blame-ignore-revs !/.github/ !/.gitignore !/.npmrc +!/.prettierignore +!/.prettierrc.js !/.release-please-manifest.json !/bin/ !/CHANGELOG* @@ -30,6 +30,7 @@ tap-testdir*/ !/tap-snapshots/ !/test/ !/tsconfig.json +tap-testdir*/ !/workspace/ /workspace/* !/workspace/test-workspace/ diff --git a/lib/config.js b/lib/config.js index f8d51dc7..a1d6fd0a 100644 --- a/lib/config.js +++ b/lib/config.js @@ -19,7 +19,16 @@ const DEFAULT_CONTENT = require.resolve(NAME) const getPkgConfig = (pkg) => pkg[CONFIG_KEY] || {} const merge = mergeWithCustomizers( - customizers.mergeArrays('branches', 'distPaths', 'allowPaths', 'ignorePaths'), + customizers.mergeArrays( + 'branches', + 'distPaths', + 'allowPaths', + 'ignorePaths', + 'lintIgnorePaths', + 'lintExtensions', + 'formatIgnorePaths', + 'formatExtensions' + ), (value, srcValue, key) => { if (key === 'ciVersions' && (Array.isArray(srcValue) || isPlainObject(srcValue))) { return { ...ciVersions.parse(value), ...ciVersions.parse(srcValue) } @@ -196,17 +205,42 @@ const getFullConfig = async ({ pkgConfig.requiredPackages.devDependencies.filter(p => !p.includes('eslint')) } + pkgConfig.lintIgnorePaths = [ + ...(pkgConfig.ignorePaths || []), + ...(pkgConfig.lintIgnorePaths || []), + ...derived.workspaceGlobs, + ] + + pkgConfig.formatIgnorePaths = [ + ...(pkgConfig.ignorePaths || []), + ...(pkgConfig.formatIgnorePaths || []), + ...derived.workspaceGlobs, + ] + if (pkgConfig.typescript) { defaultsDeep(pkgConfig, { allowPaths: [], requiredPackages: { devDependencies: [] } }) pkgConfig.distPaths = ['dist/'] + pkgConfig.lintIgnorePaths = uniq([...pkgConfig.lintIgnorePaths, 'dist/']) + pkgConfig.formatIgnorePaths = uniq([...pkgConfig.formatIgnorePaths, 'dist/']) pkgConfig.allowDistPaths = false - pkgConfig.allowPaths.push('/src/') - pkgConfig.requiredPackages.devDependencies.push( + pkgConfig.allowPaths = uniq([...pkgConfig.allowPaths, '/src/']) + pkgConfig.requiredPackages.devDependencies = uniq([ + ...pkgConfig.requiredPackages.devDependencies, 'typescript', 'tshy', '@typescript-eslint/parser', - ...derived.tap16 ? ['c8', 'ts-node'] : [] - ) + ...derived.tap16 ? ['c8', 'ts-node'] : [], + ]) + } + + if (pkgConfig.prettier) { + defaultsDeep(pkgConfig, { requiredPackages: { devDependencies: [] } }) + pkgConfig.requiredPackages.devDependencies = uniq([ + ...pkgConfig.requiredPackages.devDependencies, + 'prettier', + 'eslint-config-prettier', + '@github/prettier-config', + ]) } const gitUrl = await git.getUrl(rootPkg.path) @@ -238,24 +272,47 @@ const getFullConfig = async ({ applyRepo: !!repoFiles, applyModule: !!moduleFiles, __PARTIAL_DIRS__: fileDirs, + }) + + const ignoreAddedPaths = gitignore.sort([ + ...gitignore.allowRootDir([ + // Allways allow module files in root or workspaces + ...getAddedFiles(moduleFiles).map(s => template(s, fullConfig)), + ...(isRoot + ? [ + // in the root allow all repo files + ...getAddedFiles(repoFiles).map(s => template(s, fullConfig)), + // and allow all workspace repo level files in the root + ...pkgs + .filter(p => p.path !== rootPkg.path && p.config.workspaceRepo !== false) + .flatMap(() => getAddedFiles(files.workspaceRepo)), + ] + : []), + ]), + ...(isRoot && pkgConfig.lockfile ? ['!/package-lock.json'] : []), + ]) + + Object.assign(fullConfig, { + // Make sure we don't format any files that are being generated since they will cause template-oss-check to fail + // This could be changed if those files were also formatted before save but then we would need to read generated + // the prettier config first and use that as the formatting rules which would get weird + formatIgnorePaths: [ + ...fullConfig.formatIgnorePaths, + ...ignoreAddedPaths + .filter(f => f.startsWith('!')) + .map(f => f.replace(/^!/, '')) + .filter(f => { + const ext = extname(f).slice(1) + // ignore it if the specified format extensions match or if its a directory + return (fullConfig.formatExtensions || []).includes(ext) || (!ext && f.endsWith('/')) + }), + ], // gitignore, these use the full config so need to come at the very end ignorePaths: [ ...gitignore.sort([ - ...gitignore.allowRootDir([ - // Allways allow module files in root or workspaces - ...getAddedFiles(moduleFiles).map(s => template(s, fullConfig)), - ...isRoot ? [ - // in the root allow all repo files - ...getAddedFiles(repoFiles).map(s => template(s, fullConfig)), - // and allow all workspace repo level files in the root - ...pkgs - .filter(p => p.path !== rootPkg.path && p.config.workspaceRepo !== false) - .flatMap(() => getAddedFiles(files.workspaceRepo)), - ] : [], - ]), - ...isRoot && pkgConfig.lockfile ? ['!/package-lock.json'] : [], - ...(pkgConfig.allowPaths || []).map((p) => `!${p}`), - ...(pkgConfig.allowDistPaths ? pkgConfig.distPaths : []).map((p) => `!/${p}`), + ...ignoreAddedPaths, + ...(pkgConfig.allowPaths || []).map(p => `!${p}`), + ...(pkgConfig.allowDistPaths ? pkgConfig.distPaths : []).map(p => `!/${p}`), ...(pkgConfig.ignorePaths || []), ]), // these cant be sorted since they rely on order diff --git a/lib/content/eslintrc-js.hbs b/lib/content/eslintrc-js.hbs index bb38c366..c01ead8e 100644 --- a/lib/content/eslintrc-js.hbs +++ b/lib/content/eslintrc-js.hbs @@ -9,13 +9,9 @@ const localConfigs = readdir(__dirname) module.exports = { root: true, ignorePatterns: [ - 'tap-testdir*/', - {{#each workspaceGlobs}} + {{#each lintIgnorePaths}} '{{ . }}', {{/each}} - {{#if typescript}} - 'dist/', - {{/if}} ], {{#if typescript}} parser: '@typescript-eslint/parser', @@ -28,5 +24,8 @@ module.exports = { extends: [ '@npmcli', ...localConfigs, + {{#if prettier}} + 'prettier', + {{/if}} ], } diff --git a/lib/content/gitignore.hbs b/lib/content/gitignore.hbs index 21074e32..70aa8746 100644 --- a/lib/content/gitignore.hbs +++ b/lib/content/gitignore.hbs @@ -1,9 +1,6 @@ # ignore everything in the root /* -# transient test directories -tap-testdir*/ -# keep these {{#each ignorePaths}} {{ . }} {{/each}} diff --git a/lib/content/index.js b/lib/content/index.js index 84315218..f4f68e8a 100644 --- a/lib/content/index.js +++ b/lib/content/index.js @@ -96,6 +96,14 @@ const rootModule = { file: 'eslintrc-js.hbs', filter: (p) => p.config.eslint, }, + '.prettierrc.{{ cjsExt }}': { + file: 'prettier-js.hbs', + filter: (p) => p.config.prettier, + }, + '.prettierignore': { + file: 'prettierignore.hbs', + filter: (p) => p.config.prettier, + }, '.gitignore': 'gitignore.hbs', '.npmrc': 'npmrc.hbs', 'SECURITY.md': 'SECURITY-md.hbs', @@ -167,15 +175,21 @@ module.exports = { '/README*', '/LICENSE*', '/CHANGELOG*', + '/.git-blame-ignore-revs', ], - ignorePaths: [ - /* to be provided by consuming package */ + ignorePaths: ['tap-testdir*/'], + lintIgnorePaths: [ + // can be set by consumer ], + lintExtensions: ['js', 'cjs', 'ts', 'mjs', 'jsx', 'tsx'], + formatIgnorePaths: ['tap-snapshots/', 'test/fixtures/**/*.json'], + formatExtensions: ['js', 'cjs', 'ts', 'mjs', 'jsx', 'tsx', 'json'], ciVersions: {}, latestCiVersion: 22, lockfile: false, codeowner: '@npm/cli-team', eslint: true, + prettier: false, publish: false, typescript: false, esm: false, diff --git a/lib/content/package-json.hbs b/lib/content/package-json.hbs index a115f51e..98e7d591 100644 --- a/lib/content/package-json.hbs +++ b/lib/content/package-json.hbs @@ -3,10 +3,20 @@ "files": {{{ json distPaths }}}, "type": {{#if esm}}"module"{{else}}{{{ del }}}{{/if}}, "scripts": { - "lint": "{{#if eslint}}eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\"{{else}}echo linting disabled{{/if}}", + {{#if eslint}} + "eslint": "eslint \"**/*.{{{ extGlob lintExtensions }}}\"", + "lint": "{{ localNpmPath }} run eslint {{~#if prettier}} && {{ localNpmPath }} run prettier -- --check{{/if}}", + "lintfix": "{{ localNpmPath }} run eslint -- --fix {{~#if prettier}} && {{ localNpmPath }} run prettier -- --write{{/if}}", + {{#if prettier}} + "prettier": "prettier \"**/*.{{{ extGlob formatExtensions }}}\"", + {{/if}} + {{else}} + "eslint": {{{ del }}}, + "lint": "echo linting disabled", + "lintfix": {{{ del }}}, + {{/if}} "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "{{ localNpmPath }} run lint -- --fix", "snap": "{{#if typescript}}{{#if tap16}}c8 {{/if}}{{/if}}tap", "test": "{{#if typescript}}{{#if tap16}}c8 {{/if}}{{/if}}tap", "posttest": "{{ localNpmPath }} run lint", diff --git a/lib/content/prettier-js.hbs b/lib/content/prettier-js.hbs new file mode 100644 index 00000000..13d1ba90 --- /dev/null +++ b/lib/content/prettier-js.hbs @@ -0,0 +1,6 @@ +const githubConfig = require('@github/prettier-config') + +module.exports = { + ...githubConfig, + bracketSpacing: true, +} diff --git a/lib/content/prettierignore.hbs b/lib/content/prettierignore.hbs new file mode 100644 index 00000000..1d6d58da --- /dev/null +++ b/lib/content/prettierignore.hbs @@ -0,0 +1,3 @@ +{{#each formatIgnorePaths}} +{{ . }} +{{/each}} diff --git a/lib/util/parser.js b/lib/util/parser.js index 48b5f4e8..2156f6cd 100644 --- a/lib/util/parser.js +++ b/lib/util/parser.js @@ -174,7 +174,7 @@ class Base { } class Gitignore extends Base { - static types = ['codeowners', '.gitignore'] + static types = ['codeowners', '.gitignore', '.prettierignore'] comment = (c) => `# ${c}` } diff --git a/lib/util/template.js b/lib/util/template.js index 8a291f48..c7889a23 100644 --- a/lib/util/template.js +++ b/lib/util/template.js @@ -34,6 +34,7 @@ const makePartials = (dir, isBase) => { const setupHandlebars = (dirs) => { Handlebars.registerHelper('obj', ({ hash }) => Object.fromEntries(safeValues(hash))) + Handlebars.registerHelper('extGlob', arr => `{${arr.join(',')}}`) Handlebars.registerHelper('join', (arr, sep) => arr.join(typeof sep === 'string' ? sep : ', ')) Handlebars.registerHelper('pluck', (arr, key) => arr.map(a => a[key])) Handlebars.registerHelper('quote', (arr) => arr.map(a => `'${a}'`)) diff --git a/package.json b/package.json index ee52ed43..83cc496c 100644 --- a/package.json +++ b/package.json @@ -10,17 +10,17 @@ "template-oss-release-manager": "bin/release-manager.js" }, "scripts": { - "lint": "eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\"", - "lintfix": "npm run lint -- --fix", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "posttest": "npm run lint", "snap": "tap", "test": "tap", "template-oss-apply": "template-oss-apply --force", "postlint": "template-oss-check", "postinstall": "template-oss-apply", + "eslint": "eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\"", "test-all": "npm run test -ws -iwr --if-present", - "lint-all": "npm run lint -ws -iwr --if-present", - "test:record": "TAP_SNAPSHOT=1 NOCK_RECORD=1 tap" + "lint-all": "npm run lint -ws -iwr --if-present" }, "repository": { "type": "git", diff --git a/tap-snapshots/test/apply/source-snapshots.js.test.cjs b/tap-snapshots/test/apply/source-snapshots.js.test.cjs index a757ef1a..27e0be01 100644 --- a/tap-snapshots/test/apply/source-snapshots.js.test.cjs +++ b/tap-snapshots/test/apply/source-snapshots.js.test.cjs @@ -1231,17 +1231,17 @@ jobs: # ignore everything in the root /* -# transient test directories -tap-testdir*/ -# keep these !**/.gitignore !/.commitlintrc.js !/.eslintrc.js !/.eslintrc.local.* +!/.git-blame-ignore-revs !/.github/ !/.gitignore !/.npmrc +!/.prettierignore +!/.prettierrc.js !/.release-please-manifest.json !/bin/ !/CHANGELOG* @@ -1259,6 +1259,7 @@ tap-testdir*/ !/tap-snapshots/ !/test/ !/tsconfig.json +tap-testdir*/ .npmrc ======================================== @@ -1341,10 +1342,11 @@ package.json "name": "testpkg", "version": "1.0.0", "scripts": { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint" @@ -2887,17 +2889,17 @@ jobs: # ignore everything in the root /* -# transient test directories -tap-testdir*/ -# keep these !**/.gitignore !/.commitlintrc.js !/.eslintrc.js !/.eslintrc.local.* +!/.git-blame-ignore-revs !/.github/ !/.gitignore !/.npmrc +!/.prettierignore +!/.prettierrc.js !/.release-please-manifest.json !/bin/ !/CHANGELOG* @@ -2915,6 +2917,7 @@ tap-testdir*/ !/tap-snapshots/ !/test/ !/tsconfig.json +tap-testdir*/ !/workspaces/ /workspaces/* !/workspaces/a/ @@ -3007,10 +3010,11 @@ package.json "workspaces/b" ], "scripts": { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint", @@ -3134,13 +3138,11 @@ workspaces/a/.gitignore # ignore everything in the root /* -# transient test directories -tap-testdir*/ -# keep these !**/.gitignore !/.eslintrc.js !/.eslintrc.local.* +!/.git-blame-ignore-revs !/.gitignore !/bin/ !/CHANGELOG* @@ -3153,6 +3155,7 @@ tap-testdir*/ !/scripts/ !/tap-snapshots/ !/test/ +tap-testdir*/ workspaces/a/package.json ======================================== @@ -3160,10 +3163,11 @@ workspaces/a/package.json "name": "a", "version": "1.0.0", "scripts": { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint" @@ -3214,13 +3218,11 @@ workspaces/b/.gitignore # ignore everything in the root /* -# transient test directories -tap-testdir*/ -# keep these !**/.gitignore !/.eslintrc.js !/.eslintrc.local.* +!/.git-blame-ignore-revs !/.gitignore !/bin/ !/CHANGELOG* @@ -3233,6 +3235,7 @@ tap-testdir*/ !/scripts/ !/tap-snapshots/ !/test/ +tap-testdir*/ workspaces/b/package.json ======================================== @@ -3240,10 +3243,11 @@ workspaces/b/package.json "name": "b", "version": "1.0.0", "scripts": { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint" @@ -4498,13 +4502,11 @@ workspaces/a/.gitignore # ignore everything in the root /* -# transient test directories -tap-testdir*/ -# keep these !**/.gitignore !/.eslintrc.js !/.eslintrc.local.* +!/.git-blame-ignore-revs !/.gitignore !/bin/ !/CHANGELOG* @@ -4517,6 +4519,7 @@ tap-testdir*/ !/scripts/ !/tap-snapshots/ !/test/ +tap-testdir*/ workspaces/a/package.json ======================================== @@ -4524,10 +4527,11 @@ workspaces/a/package.json "name": "a", "version": "1.0.0", "scripts": { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint" @@ -4578,13 +4582,11 @@ workspaces/b/.gitignore # ignore everything in the root /* -# transient test directories -tap-testdir*/ -# keep these !**/.gitignore !/.eslintrc.js !/.eslintrc.local.* +!/.git-blame-ignore-revs !/.gitignore !/bin/ !/CHANGELOG* @@ -4597,6 +4599,7 @@ tap-testdir*/ !/scripts/ !/tap-snapshots/ !/test/ +tap-testdir*/ workspaces/b/package.json ======================================== @@ -4604,10 +4607,11 @@ workspaces/b/package.json "name": "b", "version": "1.0.0", "scripts": { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint" diff --git a/tap-snapshots/test/check/snapshots.js.test.cjs b/tap-snapshots/test/check/snapshots.js.test.cjs index a59000a1..88039c1b 100644 --- a/tap-snapshots/test/check/snapshots.js.test.cjs +++ b/tap-snapshots/test/check/snapshots.js.test.cjs @@ -76,10 +76,11 @@ The module file package.json needs to be updated: "lib/" ] "scripts" is missing, expected { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint" @@ -123,14 +124,16 @@ The following files are tracked by git but matching a pattern in .gitignore: To correct it: move files to not match one of the following patterns: /* - tap-testdir*/ !**/.gitignore !/.commitlintrc.js !/.eslintrc.js !/.eslintrc.local.* + !/.git-blame-ignore-revs !/.github/ !/.gitignore !/.npmrc + !/.prettierignore + !/.prettierrc.js !/.release-please-manifest.json !/bin/ !/CHANGELOG* @@ -148,6 +151,7 @@ To correct it: move files to not match one of the following patterns: !/tap-snapshots/ !/test/ !/tsconfig.json + tap-testdir*/ ------------------------------------------------------------------- ` @@ -164,14 +168,16 @@ The following files are tracked by git but matching a pattern in .gitignore: To correct it: move files to not match one of the following patterns: /* - tap-testdir*/ !**/.gitignore !/.commitlintrc.js !/.eslintrc.js !/.eslintrc.local.* + !/.git-blame-ignore-revs !/.github/ !/.gitignore !/.npmrc + !/.prettierignore + !/.prettierrc.js !/.release-please-manifest.json !/bin/ !/CHANGELOG* @@ -189,6 +195,7 @@ To correct it: move files to not match one of the following patterns: !/tap-snapshots/ !/test/ !/tsconfig.json + tap-testdir*/ !/workspaces/ /workspaces/* !/workspaces/a/ @@ -203,10 +210,10 @@ The following files are tracked by git but matching a pattern in workspaces/a/.g To correct it: move files to not match one of the following patterns: /* - tap-testdir*/ !**/.gitignore !/.eslintrc.js !/.eslintrc.local.* + !/.git-blame-ignore-revs !/.gitignore !/bin/ !/CHANGELOG* @@ -219,6 +226,7 @@ To correct it: move files to not match one of the following patterns: !/scripts/ !/tap-snapshots/ !/test/ + tap-testdir*/ ------------------------------------------------------------------- @@ -229,10 +237,10 @@ The following files are tracked by git but matching a pattern in workspaces/b/.g To correct it: move files to not match one of the following patterns: /* - tap-testdir*/ !**/.gitignore !/.eslintrc.js !/.eslintrc.local.* + !/.git-blame-ignore-revs !/.gitignore !/bin/ !/CHANGELOG* @@ -245,6 +253,7 @@ To correct it: move files to not match one of the following patterns: !/scripts/ !/tap-snapshots/ !/test/ + tap-testdir*/ ------------------------------------------------------------------- ` @@ -333,10 +342,11 @@ The module file package.json needs to be updated: "lib/" ] "scripts" is missing, expected { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint", @@ -412,10 +422,11 @@ The module file package.json needs to be updated: "lib/" ] "scripts" is missing, expected { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint" @@ -484,10 +495,11 @@ The module file package.json needs to be updated: "lib/" ] "scripts" is missing, expected { - "lint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "eslint": "eslint /"**/*.{js,cjs,ts,mjs,jsx,tsx}/"", + "lint": "npm run eslint", + "lintfix": "npm run eslint -- --fix", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", "snap": "tap", "test": "tap", "posttest": "npm run lint" diff --git a/test/apply/eslint.js b/test/apply/eslint.js deleted file mode 100644 index 56692818..00000000 --- a/test/apply/eslint.js +++ /dev/null @@ -1,24 +0,0 @@ -const t = require('tap') -const setup = require('../setup.js') - -t.test('can disable eslint', async (t) => { - const s = await setup(t, { - package: { - templateOSS: { - eslint: false, - }, - }, - }) - await s.apply() - - const pkg = await s.readJson('package.json') - delete pkg.templateOSS // templateOSS config has eslint in it - t.notMatch(JSON.stringify(pkg), 'eslint') - - const gitignore = await s.readFile('.gitignore') - t.notMatch(gitignore, 'eslint') - - const checks = await s.check() - t.equal(checks.length, 1) - t.notMatch(checks[0].solution, 'eslint') -}) diff --git a/test/apply/lint.js b/test/apply/lint.js new file mode 100644 index 00000000..a1b8d94b --- /dev/null +++ b/test/apply/lint.js @@ -0,0 +1,61 @@ +const t = require('tap') +const setup = require('../setup.js') + +t.test('can disable eslint', async (t) => { + const s = await setup(t, { + package: { + templateOSS: { + eslint: false, + }, + }, + }) + await s.apply() + + const pkg = await s.readJson('package.json') + delete pkg.templateOSS // templateOSS config has eslint in it + t.notMatch(JSON.stringify(pkg), 'eslint') + + const gitignore = await s.readFile('.gitignore') + t.notMatch(gitignore, 'eslint') + + const checks = await s.check() + t.equal(checks.length, 1) + t.notMatch(checks[0].solution, 'eslint') +}) + +t.test('can enable prettier', async (t) => { + const s = await setup(t, { + ok: true, + package: { + templateOSS: { + prettier: true, + }, + }, + }) + await s.apply() + + const pkg = await s.readJson('package.json') + t.ok(pkg.scripts.prettier) + t.match(pkg.scripts.lint, 'npm run prettier') + t.match(pkg.scripts.lintfix, 'npm run prettier') + + const checks = await s.check() + t.equal(checks.length, 1) + t.match(checks[0].body, [ + 'prettier', + 'eslint-config-prettier', + '@github/prettier-config', + ]) + + await s.writeJson('package.json', { + ...pkg, + devDependencies: { + ...pkg.devDependencies, + prettier: '^3.0.0', + 'eslint-config-prettier': '^9.0.0', + '@github/prettier-config': '0.0.6', + }, + }) + + t.strictSame(await s.check(), []) +}) diff --git a/workspace/test-workspace/.gitignore b/workspace/test-workspace/.gitignore index a96d056a..8591cc37 100644 --- a/workspace/test-workspace/.gitignore +++ b/workspace/test-workspace/.gitignore @@ -2,13 +2,11 @@ # ignore everything in the root /* -# transient test directories -tap-testdir*/ -# keep these !**/.gitignore !/.eslintrc.js !/.eslintrc.local.* +!/.git-blame-ignore-revs !/.gitignore !/bin/ !/CHANGELOG* @@ -21,3 +19,4 @@ tap-testdir*/ !/scripts/ !/tap-snapshots/ !/test/ +tap-testdir*/ diff --git a/workspace/test-workspace/package.json b/workspace/test-workspace/package.json index cd0d379d..4acf9648 100644 --- a/workspace/test-workspace/package.json +++ b/workspace/test-workspace/package.json @@ -6,12 +6,13 @@ "private": true, "scripts": { "test": "tap", - "lint": "eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\"", + "lint": "npm run eslint", "postlint": "template-oss-check", "template-oss-apply": "template-oss-apply --force", - "lintfix": "npm run lint -- --fix", + "lintfix": "npm run eslint -- --fix", "snap": "tap", - "posttest": "npm run lint" + "posttest": "npm run lint", + "eslint": "eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\"" }, "devDependencies": { "@npmcli/eslint-config": "^4.0.0",