Skip to content

Commit

Permalink
fix: run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed May 17, 2024
1 parent 210247e commit b35bca5
Show file tree
Hide file tree
Showing 73 changed files with 928 additions and 1,004 deletions.
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
28 changes: 14 additions & 14 deletions lib/check/check-gitignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ const NAME = 'check-gitignore'
const run = async ({ root, path, config }) => {
log.verbose(NAME, { root, path })

const relativeToRoot = (f) => relative(root, resolve(path, f))
const relativeToRoot = f => relative(root, resolve(path, f))

// use the root to detect a git repo but the project directory (path) for the
// ignore check
const ignoreFile = resolve(path, '.gitignore')
if (!await git.is({ cwd: root }) || !existsSync(ignoreFile)) {
if (!(await git.is({ cwd: root })) || !existsSync(ignoreFile)) {
log.verbose(NAME, 'no git or no gitignore')
return null
}

log.verbose(NAME, `using ignore file ${ignoreFile}`)

const res = await git.spawn([
'ls-files',
'--cached',
'--ignored',
// https://git-scm.com/docs/git-ls-files#_exclude_patterns
`--${config.isRoot ? 'exclude-from' : 'exclude-per-directory'}=${basename(ignoreFile)}`,
], { cwd: path })
const res = await git.spawn(
[
'ls-files',
'--cached',
'--ignored',
// https://git-scm.com/docs/git-ls-files#_exclude_patterns
`--${config.isRoot ? 'exclude-from' : 'exclude-per-directory'}=${basename(ignoreFile)}`,
],
{ cwd: path },
)

log.verbose(NAME, 'ls-files', res)

// TODO: files should be filtered if they have already been moved/deleted
// but not committed. Currently you must commit for this check to pass.
const files = res.stdout
.trim()
.split('\n')
.filter(Boolean)
const files = res.stdout.trim().split('\n').filter(Boolean)

if (!files.length) {
return null
Expand All @@ -49,7 +49,7 @@ const run = async ({ root, path, config }) => {
const ignores = (await fs.readFile(ignoreFile))
.toString()
.split(/\r?\n/)
.filter((l) => l && !l.trim().startsWith('#'))
.filter(l => l && !l.trim().startsWith('#'))

const relIgnore = relativeToRoot(ignoreFile)

Expand Down
28 changes: 13 additions & 15 deletions lib/check/check-required.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@ const npa = require('npm-package-arg')
const { partition } = require('lodash')
const hasPackage = require('../util/has-package.js')

const rmCommand = (specs) =>
`npm rm ${specs.map((s) => s.name).join(' ')}`.trim()
const rmCommand = specs => `npm rm ${specs.map(s => s.name).join(' ')}`.trim()

const installCommand = (specs, flags) => specs.length ?
`npm i ${specs.map((s) => `${s.name}@${s.fetchSpec}`).join(' ')} ${flags.join(' ')}`.trim() : ''
const installCommand = (specs, flags) =>
specs.length ? `npm i ${specs.map(s => `${s.name}@${s.fetchSpec}`).join(' ')} ${flags.join(' ')}`.trim() : ''

// ensure required packages are present in the correct place
const run = ({ pkg, path, config: { requiredPackages = {} } }) => {
// keys are the dependency location in package.json
// values are a filtered list of parsed specs that dont exist in the current package
// { [location]: [spec1, spec2] }
const requiredByLocation = Object.entries(requiredPackages)
.reduce((acc, [location, pkgs]) => {
acc[location] = pkgs
.filter((spec) => !hasPackage(pkg, spec, [location], path))
.map((spec) => npa(spec))
log.verbose(location, pkg, pkgs)
return acc
}, {})
const requiredByLocation = Object.entries(requiredPackages).reduce((acc, [location, pkgs]) => {
acc[location] = pkgs.filter(spec => !hasPackage(pkg, spec, [location], path)).map(spec => npa(spec))
log.verbose(location, pkg, pkgs)
return acc
}, {})

const requiredEntries = Object.entries(requiredByLocation)

Expand All @@ -30,19 +26,21 @@ const run = ({ pkg, path, config: { requiredPackages = {} } }) => {
if (requiredEntries.flatMap(([, specs]) => specs).length) {
return requiredEntries.map(([location, specs]) => {
const locationFlag = hasPackage.flags[location]
const [exactSpecs, saveSpecs] = partition(specs, (s) => s.type === 'version')
const [exactSpecs, saveSpecs] = partition(specs, s => s.type === 'version')

log.verbose('check-required', location, specs)

return {
title: `The following required ${location} were not found:`,
body: specs.map((s) => s.rawSpec === '*' ? s.name : `${s.name}@${s.rawSpec}`),
body: specs.map(s => (s.rawSpec === '*' ? s.name : `${s.name}@${s.rawSpec}`)),
// solution is to remove any existing all at once but add back in by --save-<location>
solution: [
rmCommand(specs),
installCommand(saveSpecs, [locationFlag]),
installCommand(exactSpecs, [locationFlag, '--save-exact']),
].filter(Boolean).join(' && '),
]
.filter(Boolean)
.join(' && '),
}
})
}
Expand Down

0 comments on commit b35bca5

Please sign in to comment.