Skip to content

Commit

Permalink
feat: add prettier support
Browse files Browse the repository at this point in the history
Co-authored-by: Julian Møller Ellehauge <git@jumoel.com>
  • Loading branch information
lukekarrys and jumoel committed May 17, 2024
1 parent 8aef509 commit 60ee94f
Show file tree
Hide file tree
Showing 17 changed files with 253 additions and 112 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand All @@ -30,6 +30,7 @@ tap-testdir*/
!/tap-snapshots/
!/test/
!/tsconfig.json
tap-testdir*/
!/workspace/
/workspace/*
!/workspace/test-workspace/
97 changes: 77 additions & 20 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/content/eslintrc-js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -28,5 +24,8 @@ module.exports = {
extends: [
'@npmcli',
...localConfigs,
{{#if prettier}}
'prettier',
{{/if}}
],
}
3 changes: 0 additions & 3 deletions lib/content/gitignore.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# ignore everything in the root
/*
# transient test directories
tap-testdir*/

# keep these
{{#each ignorePaths}}
{{ . }}
{{/each}}
18 changes: 16 additions & 2 deletions lib/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions lib/content/package-json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions lib/content/prettier-js.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const githubConfig = require('@github/prettier-config')

module.exports = {
...githubConfig,
bracketSpacing: true,
}
3 changes: 3 additions & 0 deletions lib/content/prettierignore.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each formatIgnorePaths}}
{{ . }}
{{/each}}
2 changes: 1 addition & 1 deletion lib/util/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Base {
}

class Gitignore extends Base {
static types = ['codeowners', '.gitignore']
static types = ['codeowners', '.gitignore', '.prettierignore']
comment = (c) => `# ${c}`
}

Expand Down
1 change: 1 addition & 0 deletions lib/util/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`))
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 60ee94f

Please sign in to comment.