Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prettier support #447

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module.exports = {
extends: [
'@npmcli',
...localConfigs,
'prettier',
],
}
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/
14 changes: 14 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file is automatically added by @npmcli/template-oss. Do not edit.

tap-testdir*/
tap-snapshots/
test/fixtures/**/*.json
workspace/test-workspace/**
/.commitlintrc.js
/.eslintrc.js
/.github/
/.prettierrc.js
/.release-please-manifest.json
/package.json
/release-please-config.json
/tsconfig.json
8 changes: 8 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* This file is automatically added by @npmcli/template-oss. Do not edit. */

const githubConfig = require('@github/prettier-config')

module.exports = {
...githubConfig,
bracketSpacing: true,
}
7 changes: 2 additions & 5 deletions bin/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
const apply = require('../lib/apply/index.js')

const main = async () => {
const {
npm_config_global: globalMode,
npm_config_local_prefix: root,
} = process.env
const { npm_config_global: globalMode, npm_config_local_prefix: root } = process.env

// do nothing in global mode or when the local prefix isn't set
if (globalMode === 'true' || !root) {
Expand All @@ -16,7 +13,7 @@ const main = async () => {
await apply(root)
}

module.exports = main().catch((err) => {
module.exports = main().catch(err => {
console.error(err.stack)
process.exitCode = 1
})
6 changes: 2 additions & 4 deletions bin/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ const check = require('../lib/check/index.js')
const output = require('../lib/util/output.js')

const main = async () => {
const {
npm_config_local_prefix: root,
} = process.env
const { npm_config_local_prefix: root } = process.env

if (!root) {
throw new Error('This package requires npm >7.21.1')
Expand All @@ -20,7 +18,7 @@ const main = async () => {
}
}

module.exports = main().catch((err) => {
module.exports = main().catch(err => {
console.error(err.stack)
process.exitCode = 1
})
2 changes: 1 addition & 1 deletion bin/release-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ReleaseManager.run({
},
}).values,
})
.then((result) => {
.then(result => {
core.setOutput('result', result)
return null
})
Expand Down
40 changes: 22 additions & 18 deletions bin/release-please.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ ReleasePlease.run({
// `RELEASE_PLEASE_<manfiestOverrideConfigName>`
// (eg`RELEASE_PLEASE_lastReleaseSha=<SHA>`) to set one-off config items for
// the release please run without needing to commit and push the config.
overrides: Object.fromEntries(Object.entries(process.env)
.filter(([k, v]) => k.startsWith('RELEASE_PLEASE_') && v != null)
.map(([k, v]) => [k.replace('RELEASE_PLEASE_', ''), v])),
}).then(({ pr, releases }) => {
if (pr) {
core.setOutput('pr', JSON.stringify(pr))
core.setOutput('pr-branch', pr.headBranchName)
core.setOutput('pr-number', pr.number)
core.setOutput('pr-sha', pr.sha)
}
overrides: Object.fromEntries(
Object.entries(process.env)
.filter(([k, v]) => k.startsWith('RELEASE_PLEASE_') && v != null)
.map(([k, v]) => [k.replace('RELEASE_PLEASE_', ''), v]),
),
})
.then(({ pr, releases }) => {
if (pr) {
core.setOutput('pr', JSON.stringify(pr))
core.setOutput('pr-branch', pr.headBranchName)
core.setOutput('pr-number', pr.number)
core.setOutput('pr-sha', pr.sha)
}

if (releases) {
core.setOutput('releases', JSON.stringify(releases))
}
if (releases) {
core.setOutput('releases', JSON.stringify(releases))
}

return null
}).catch(err => {
core.setFailed('Release Please failed')
core.error(err)
})
return null
})
.catch(err => {
core.setFailed('Release Please failed')
core.error(err)
})
33 changes: 14 additions & 19 deletions lib/apply/apply-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@ const run = async (dir, files, options) => {
const { rm, add } = files

log.verbose('apply-files', 'rm', rm)
await rmEach(dir, rm, options, (f) => fs.rm(f))
await rmEach(dir, rm, options, f => fs.rm(f))

log.verbose('apply-files', 'add', add)
await parseEach(dir, add, options, {}, (p) => p.applyWrite())
await parseEach(dir, add, options, {}, p => p.applyWrite())
}

module.exports = [{
run: (options) => run(
options.config.repoDir,
options.config.repoFiles,
options
),
when: ({ config: c }) => c.applyRepo && c.needsUpdate,
name: 'apply-repo',
}, {
run: (options) => run(
options.config.moduleDir,
options.config.moduleFiles,
options
),
when: ({ config: c }) => c.applyModule && c.needsUpdate,
name: 'apply-module',
}]
module.exports = [
{
run: options => run(options.config.repoDir, options.config.repoFiles, options),
when: ({ config: c }) => c.applyRepo && c.needsUpdate,
name: 'apply-repo',
},
{
run: options => run(options.config.moduleDir, options.config.moduleFiles, options),
when: ({ config: c }) => c.applyModule && c.needsUpdate,
name: 'apply-module',
},
]
6 changes: 1 addition & 5 deletions lib/apply/apply-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ const { log } = require('proc-log')
const PackageJson = require('@npmcli/package-json')

const run = async ({ config: c }) => {
const {
moduleDir: dir,
__CONFIG_KEY__: key,
__VERSION__: version,
} = c
const { moduleDir: dir, __CONFIG_KEY__: key, __VERSION__: version } = c

log.verbose('apply-version', dir)

Expand Down
5 changes: 1 addition & 4 deletions lib/apply/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const run = require('../index.js')

module.exports = (root) => run(root, [
require('./apply-files.js'),
require('./apply-version.js'),
])
module.exports = root => run(root, [require('./apply-files.js'), require('./apply-version.js')])
76 changes: 38 additions & 38 deletions lib/check/check-apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ const solution = 'npx template-oss-apply --force'

const run = async (type, dir, files, options) => {
const res = []
const rel = (f) => relative(options.root, f)
const rel = f => relative(options.root, f)
const { add: addFiles, rm: rmFiles } = files

const rm = await rmEach(dir, rmFiles, options, (f) => rel(f))
const [add, update] = partition(await parseEach(dir, addFiles, options, {}, async (p) => {
const diff = await p.applyDiff()
const target = rel(p.target)
if (diff === null) {
// needs to be added
return target
} else if (diff === true) {
// its ok, no diff, this is filtered out
return null
}
return { file: target, diff }
}), (d) => typeof d === 'string')
const rm = await rmEach(dir, rmFiles, options, f => rel(f))
const [add, update] = partition(
await parseEach(dir, addFiles, options, {}, async p => {
const diff = await p.applyDiff()
const target = rel(p.target)
if (diff === null) {
// needs to be added
return target
} else if (diff === true) {
// its ok, no diff, this is filtered out
return null
}
return { file: target, diff }
}),
d => typeof d === 'string',
)

log.verbose('check-apply', 'rm', rm)
if (rm.length) {
Expand All @@ -44,31 +47,28 @@ const run = async (type, dir, files, options) => {
}

log.verbose('check-apply', 'update', update)
res.push(...update.sort((a, b) => localeCompare(a.file, b.file)).map(({ file, diff }) => ({
title: `The ${type} file ${basename(file)} needs to be updated:`,
body: [`${file}\n${'='.repeat(40)}\n${diff}`],
solution,
})))
res.push(
...update
.sort((a, b) => localeCompare(a.file, b.file))
.map(({ file, diff }) => ({
title: `The ${type} file ${basename(file)} needs to be updated:`,
body: [`${file}\n${'='.repeat(40)}\n${diff}`],
solution,
})),
)

return res
}

module.exports = [{
run: (options) => run(
'repo',
options.config.repoDir,
options.config.repoFiles,
options
),
when: ({ config: c }) => c.applyRepo,
name: 'check-repo',
}, {
run: (options) => run(
'module',
options.config.moduleDir,
options.config.moduleFiles,
options
),
when: ({ config: c }) => c.applyModule,
name: 'check-module',
}]
module.exports = [
{
run: options => run('repo', options.config.repoDir, options.config.repoFiles, options),
when: ({ config: c }) => c.applyRepo,
name: 'check-repo',
},
{
run: options => run('module', options.config.moduleDir, options.config.moduleFiles, options),
when: ({ config: c }) => c.applyModule,
name: 'check-module',
},
]
5 changes: 1 addition & 4 deletions lib/check/check-changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ const run = async ({ root, path }) => {
if (!mustStart.test(content)) {
return {
title: `The ${relative(root, changelog)} is incorrect:`,
body: [
'The changelog should start with',
`"# Changelog\n\n#"`,
],
body: ['The changelog should start with', `"# Changelog\n\n#"`],
solution: 'reformat the changelog to have the correct heading',
}
}
Expand Down
11 changes: 5 additions & 6 deletions lib/check/check-engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ const run = async ({ root, path, pkg, config: { omitEngines = [] } }) => {
}

if (invalid.length) {
const title = `The following production dependencies are not compatible with ` +
const title =
`The following production dependencies are not compatible with ` +
`\`engines.node: ${engines}\` found in \`${pkgPath}\`:`
return {
title,
body: invalid.map((dep) => [
`${dep.name}:`,
` engines.node: ${dep.engines}`,
` location: ${dep.location}`,
].join('\n')).join('\n'),
body: invalid
.map(dep => [`${dep.name}:`, ` engines.node: ${dep.engines}`, ` location: ${dep.location}`].join('\n'))
.join('\n'),
solution: 'Remove them or move them to devDependencies.',
}
}
Expand Down
Loading
Loading