@jcoreio/toolchain 4.0.1 → 4.2.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcoreio/toolchain",
3
- "version": "4.0.1",
3
+ "version": "4.2.0",
4
4
  "description": "base JS build toolchain",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,7 @@
12
12
  "bugs": {
13
13
  "url": "https://github.com/jcoreio/toolchains/issues"
14
14
  },
15
- "homepage": "https://github.com/jcoreio/toolchains/tree/beta/packages/base",
15
+ "homepage": "https://github.com/jcoreio/toolchains/tree/main/packages/base",
16
16
  "dependencies": {
17
17
  "@jcoreio/eslint-plugin-implicit-dependencies": "^1.1.1",
18
18
  "chalk": "^4.0.0",
@@ -2,6 +2,7 @@ const { name } = require('../package.json')
2
2
  const dedent = require('dedent-js')
3
3
  const fs = require('../util/projectFs.cjs')
4
4
  const JSON5 = require('json5')
5
+ const { isMonorepoSubpackage } = require('../util/findUps.cjs')
5
6
  const getPluginsArraySync = require('../util/getPluginsArraySync.cjs')
6
7
 
7
8
  async function getRootEslintConfig() {
@@ -60,7 +61,7 @@ module.exports = [
60
61
  },
61
62
  }
62
63
  for (const file of [
63
- 'githooks.cjs',
64
+ ...(isMonorepoSubpackage ? [] : ['githooks.cjs']),
64
65
  'lint-staged.config.cjs',
65
66
  'prettier.config.cjs',
66
67
  ]) {
@@ -0,0 +1,259 @@
1
+ const path = require('path')
2
+ const execa = require('../util/execa.cjs')
3
+ const ChdirFs = require('../util/ChdirFs.cjs')
4
+ const pkg = require('../package.json')
5
+ const dedent = require('dedent-js')
6
+ const parseRepositoryUrl = require('../util/parseRepositoryUrl.cjs')
7
+
8
+ async function create(args = []) {
9
+ const prompt = require('prompts')
10
+
11
+ let monorepoPackageJson, monorepoProjectDir
12
+ try {
13
+ ;({
14
+ monorepoPackageJson,
15
+ monorepoProjectDir,
16
+ } = require('../util/findUps.cjs'))
17
+ } catch (error) {
18
+ if (!error.message.startsWith('failed to find project package.json')) {
19
+ throw error
20
+ }
21
+ }
22
+
23
+ const required = (s) => Boolean(s) || 'required'
24
+
25
+ let defaultAuthor
26
+ if (monorepoPackageJson) defaultAuthor = monorepoPackageJson.author
27
+ else {
28
+ try {
29
+ defaultAuthor = (
30
+ await execa('git', ['config', 'user.name'], {
31
+ stdio: 'pipe',
32
+ maxBuffer: 1024,
33
+ encoding: 'utf8',
34
+ })
35
+ ).stdout.trim()
36
+ } catch (error) {
37
+ // ignore
38
+ }
39
+ }
40
+
41
+ const questions = [
42
+ {
43
+ type: 'text',
44
+ name: 'directory',
45
+ message: 'Destination directory:',
46
+ validate: required,
47
+ },
48
+ {
49
+ type: 'text',
50
+ name: 'name',
51
+ message: 'Package name:',
52
+ initial: (prev, { directory }) => path.basename(directory),
53
+ validate: required,
54
+ },
55
+ {
56
+ type: 'text',
57
+ name: 'description',
58
+ message: 'Package description:',
59
+ validate: required,
60
+ },
61
+ {
62
+ type: 'text',
63
+ name: 'author',
64
+ initial: defaultAuthor,
65
+ message: 'Package author:',
66
+ validate: required,
67
+ },
68
+ {
69
+ type: 'text',
70
+ name: 'keywords',
71
+ message: 'Package keywords:',
72
+ format: (text) => (text || '').split(/\s*,\s*|\s+/g),
73
+ },
74
+ ...(monorepoPackageJson
75
+ ? []
76
+ : [
77
+ {
78
+ type: 'text',
79
+ name: 'organization',
80
+ initial: (prev, { name }) => {
81
+ const match = /^@(.*?)\//.exec(name)
82
+ if (match) return match[1]
83
+ },
84
+ message: 'GitHub organization:',
85
+ validate: required,
86
+ },
87
+ {
88
+ type: 'text',
89
+ name: 'repo',
90
+ message: 'GitHub repo:',
91
+ initial: (prev, { name }) => name.replace(/^@(.*?)\//, ''),
92
+ validate: required,
93
+ },
94
+ ]),
95
+ {
96
+ type: 'select',
97
+ name: 'license',
98
+ message: 'License',
99
+ choices: Object.entries(licenses).map(([value, { title }]) => ({
100
+ title,
101
+ value,
102
+ })),
103
+ },
104
+ {
105
+ type: 'text',
106
+ name: 'copyrightHolder',
107
+ message: 'Copyright holder:',
108
+ initial: (prev, { author }) => author,
109
+ validate: required,
110
+ },
111
+ {
112
+ name: 'ready',
113
+ type: 'confirm',
114
+ initial: true,
115
+ message: 'Ready to go?',
116
+ },
117
+ ]
118
+
119
+ let answers
120
+ do {
121
+ answers = await prompt(questions)
122
+ for (const question of questions) {
123
+ const { name, transformer } = question
124
+ if (name !== 'ready') {
125
+ const answer = answers[name]
126
+ question.initial = transformer ? transformer(answer) : answer
127
+ }
128
+ }
129
+ } while (!answers.ready)
130
+
131
+ const {
132
+ directory,
133
+ name,
134
+ description,
135
+ author,
136
+ keywords,
137
+ license,
138
+ copyrightHolder,
139
+ } = answers
140
+
141
+ const { organization, repo } = monorepoPackageJson
142
+ ? parseRepositoryUrl(monorepoPackageJson.repository.url)
143
+ : answers
144
+
145
+ const cwd = path.resolve(directory)
146
+
147
+ await require('fs-extra').mkdirs(path.join(cwd, 'src'))
148
+
149
+ const fs = ChdirFs(cwd)
150
+
151
+ const subpackagePath = monorepoProjectDir
152
+ ? path.relative(monorepoProjectDir, cwd)
153
+ : undefined
154
+
155
+ const branch = subpackagePath
156
+ ? (
157
+ await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
158
+ stdio: 'pipe',
159
+ encoding: 'utf8',
160
+ maxBuffer: 1024,
161
+ })
162
+ ).stdout.trim()
163
+ : ''
164
+
165
+ const packageJson = {
166
+ name,
167
+ description,
168
+ repository: {
169
+ type: 'git',
170
+ url: `https://github.com/${organization}/${repo}.git`,
171
+ ...(subpackagePath ? { directory: subpackagePath } : {}),
172
+ },
173
+ homepage: `https://github.com/${organization}/${repo}${
174
+ subpackagePath ? `/tree/${branch}/${subpackagePath}` : ''
175
+ }`,
176
+ bugs: {
177
+ url: `https://github.com/${organization}/${repo}/issues`,
178
+ },
179
+ author,
180
+ license,
181
+ keywords,
182
+ }
183
+
184
+ await fs.writeJson('package.json', packageJson, { spaces: 2 })
185
+
186
+ const files = {
187
+ 'README.md': dedent`
188
+ # ${name}
189
+
190
+ ${description}
191
+
192
+ [![CircleCI](https://circleci.com/gh/${organization}/${repo}.svg?style=svg)](https://circleci.com/gh/${organization}/${repo})
193
+ [![Coverage Status](https://codecov.io/gh/${organization}/${repo}/branch/master/graph/badge.svg)](https://codecov.io/gh/${organization}/${repo})
194
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
195
+ [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
196
+ [![npm version](https://badge.fury.io/js/${encodeURIComponent(
197
+ name
198
+ )}.svg)](https://badge.fury.io/js/${encodeURIComponent(name)})
199
+ `,
200
+ 'LICENSE.md': (
201
+ await fs.readFile(require.resolve(`./licenses/${license}.md`), 'utf8')
202
+ )
203
+ .replace(/<YEAR>/g, String(new Date().getFullYear()))
204
+ .replace(/<COPYRIGHT HOLDER>/g, copyrightHolder),
205
+ }
206
+ await Promise.all(
207
+ Object.entries(files).map(async ([name, content]) => {
208
+ const file = path.resolve(cwd, name)
209
+ await fs.writeFile(file, content, 'utf8')
210
+ // eslint-disable-next-line no-console
211
+ console.error(`wrote ${path.relative(process.cwd(), file)}`)
212
+ })
213
+ )
214
+
215
+ if (!monorepoPackageJson) {
216
+ await execa('git', ['init'], { cwd })
217
+ await execa(
218
+ 'git',
219
+ ['remote', 'add', 'origin', packageJson.repository.url],
220
+ {
221
+ cwd,
222
+ }
223
+ )
224
+ }
225
+
226
+ const isTest = Boolean(process.env.JCOREIO_TOOLCHAIN_SELF_TEST)
227
+
228
+ await execa(
229
+ 'pnpm',
230
+ [
231
+ 'add',
232
+ '-D',
233
+ '--prefer-offline',
234
+ `${pkg.name}@${isTest ? 'workspace:*' : pkg.version}`,
235
+ ],
236
+ { cwd }
237
+ )
238
+ await execa('pnpm', ['exec', 'tc', 'init'], { cwd })
239
+
240
+ if (!monorepoPackageJson) {
241
+ await execa('git', ['add', '.'], { cwd })
242
+ await execa(
243
+ 'git',
244
+ ['commit', '-m', 'chore: initial commit from tc create'],
245
+ {
246
+ cwd,
247
+ }
248
+ )
249
+ }
250
+ }
251
+
252
+ exports.description = 'create a new toolchain project'
253
+ exports.run = create
254
+
255
+ const licenses = {
256
+ MIT: {
257
+ title: 'The MIT License',
258
+ },
259
+ }
package/scripts/init.cjs CHANGED
@@ -69,7 +69,6 @@ async function init(args = []) {
69
69
  await execa('pnpm', [
70
70
  'add',
71
71
  '-D',
72
- '--no-optional',
73
72
  '--prefer-offline',
74
73
  ...(isMonorepoRoot ? ['-w'] : []),
75
74
  isTest ? '../packages/base' : `${name}@^${version}`,
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) <YEAR> <COPYRIGHT HOLDER>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -34,7 +34,6 @@ async function migrate(args = []) {
34
34
  if (!args.includes('--config-only')) {
35
35
  await execa('pnpm', [
36
36
  'i',
37
- '--no-optional',
38
37
  '--prefer-offline',
39
38
  '--fix-lockfile',
40
39
  ...(isMonorepoRoot ? ['-w'] : []),
@@ -1,12 +1,17 @@
1
1
  const execa = require('../util/execa.cjs')
2
+ const { isMonorepoRoot, packageJson } = require('../util/findUps.cjs')
2
3
 
3
4
  exports.run = async function (args = []) {
4
- const { scripts } = require('./toolchain.cjs')
5
- await execa('tc', ['check'])
6
- if (scripts.coverage) await execa('tc', ['coverage'])
7
- if (scripts['test:esm']) await execa('tc', ['test:esm'])
8
- await execa('tc', ['build'])
9
- await execa('tc', ['build:smoke-test'])
5
+ if (isMonorepoRoot && packageJson.name !== '@jcoreio/toolchains') {
6
+ await execa('pnpm', ['run', '-r', 'prepublish'])
7
+ } else {
8
+ const { scripts } = require('./toolchain.cjs')
9
+ await execa('tc', ['check'])
10
+ if (scripts.coverage) await execa('tc', ['coverage'])
11
+ if (scripts['test:esm']) await execa('tc', ['test:esm'])
12
+ await execa('tc', ['build'])
13
+ await execa('tc', ['build:smoke-test'])
14
+ }
10
15
  }
11
16
 
12
17
  exports.description = 'run check, coverage, and build'
@@ -3,45 +3,57 @@
3
3
  const { name, version } = require('../package.json')
4
4
  const chalk = require('chalk')
5
5
  const getPluginsObjectSync = require('../util/getPluginsObjectSync.cjs')
6
- const { toolchainConfig } = require('../util/findUps.cjs')
7
6
  const execa = require('../util/execa.cjs')
8
7
 
9
- const scripts = {
10
- migrate: require('./migrate.cjs'),
11
- build: require('./build.cjs'),
12
- 'build:smoke-test': require('./smokeTestBuild.cjs'),
13
- check: require('./check.cjs'),
14
- clean: require('./clean.cjs'),
15
- format: require('./format.cjs'),
16
- init: require('./init.cjs'),
17
- preinstall: require('./preinstall.cjs'),
18
- lint: require('./lint.cjs'),
19
- 'lint:fix': require('./lint-fix.cjs'),
20
- 'open:coverage': require('./open-coverage.cjs'),
21
- prepublish: require('./prepublish.cjs'),
22
- upgrade: require('./upgrade.cjs'),
23
- version: {
24
- description: `print version of ${name}`,
25
- run: () => {
26
- // eslint-disable-next-line no-console
27
- console.log(`${name}@${version}`)
28
- },
29
- },
30
- 'install-git-hooks': require('./install-git-hooks.cjs'),
31
- ...getPluginsObjectSync('scripts'),
32
- ...Object.fromEntries(
33
- Object.entries(toolchainConfig.scripts || {}).map(([name, script]) => [
34
- name,
35
- typeof script === 'string'
36
- ? {
37
- run: (args = []) =>
38
- execa([script, ...args].join(' '), { shell: true }),
39
- description: script,
40
- }
41
- : script,
42
- ])
43
- ),
8
+ let toolchainConfig, isMonorepoRoot
9
+ try {
10
+ ;({ toolchainConfig, isMonorepoRoot } = require('../util/findUps.cjs'))
11
+ } catch (error) {
12
+ if (!error.message.startsWith('failed to find project package.json')) {
13
+ throw error
14
+ }
44
15
  }
16
+ const scripts = toolchainConfig
17
+ ? {
18
+ migrate: require('./migrate.cjs'),
19
+ build: require('./build.cjs'),
20
+ 'build:smoke-test': require('./smokeTestBuild.cjs'),
21
+ check: require('./check.cjs'),
22
+ clean: require('./clean.cjs'),
23
+ format: require('./format.cjs'),
24
+ init: require('./init.cjs'),
25
+ preinstall: require('./preinstall.cjs'),
26
+ lint: require('./lint.cjs'),
27
+ 'lint:fix': require('./lint-fix.cjs'),
28
+ 'open:coverage': require('./open-coverage.cjs'),
29
+ prepublish: require('./prepublish.cjs'),
30
+ upgrade: require('./upgrade.cjs'),
31
+ version: {
32
+ description: `print version of ${name}`,
33
+ run: () => {
34
+ // eslint-disable-next-line no-console
35
+ console.log(`${name}@${version}`)
36
+ },
37
+ },
38
+ 'install-git-hooks': require('./install-git-hooks.cjs'),
39
+ ...(isMonorepoRoot ? { create: require('./create.cjs') } : {}),
40
+ ...getPluginsObjectSync('scripts'),
41
+ ...Object.fromEntries(
42
+ Object.entries(toolchainConfig.scripts || {}).map(([name, script]) => [
43
+ name,
44
+ typeof script === 'string'
45
+ ? {
46
+ run: (args = []) =>
47
+ execa([script, ...args].join(' '), { shell: true }),
48
+ description: script,
49
+ }
50
+ : script,
51
+ ])
52
+ ),
53
+ }
54
+ : {
55
+ create: require('./create.cjs'),
56
+ }
45
57
 
46
58
  exports.scripts = scripts
47
59
 
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const { packageJson, isMonorepoRoot } = require('../util/findUps.cjs')
3
+ const { packageJson, monorepoProjectDir } = require('../util/findUps.cjs')
4
4
  const execa = require('../util/execa.cjs')
5
5
  const { name } = require('../package.json')
6
6
 
@@ -19,18 +19,27 @@ async function upgrade([version] = []) {
19
19
  ).stdout.trim()
20
20
  }
21
21
 
22
- await execa('pnpm', [
23
- 'add',
24
- '-D',
25
- '--no-optional',
26
- '--prefer-offline',
27
- ...(isMonorepoRoot ? ['-w'] : []),
28
- isTest ? '../packages/base' : `${name}@^${version}`,
29
- ...(isTest
30
- ? toolchains.map((t) => t.replace(`${name}-`, '../packages/'))
31
- : toolchains.map((t) => `${t}@^${version}`)),
32
- ])
33
- await execa('tc', ['migrate'])
22
+ await execa(
23
+ 'pnpm',
24
+ isTest
25
+ ? [
26
+ ...(monorepoProjectDir ? ['-r'] : []),
27
+ 'add',
28
+ '-D',
29
+ '--prefer-offline',
30
+ '../packages/base',
31
+ ...toolchains.map((t) => t.replace(`${name}-`, '../packages/')),
32
+ ]
33
+ : [
34
+ ...(monorepoProjectDir ? ['-r'] : []),
35
+ 'update',
36
+ '--prefer-offline',
37
+ `${name}@^${version}`,
38
+ toolchains.map((t) => `${t}@^${version}`),
39
+ ]
40
+ )
41
+ if (monorepoProjectDir) await execa('pnpm', ['-r', 'run', 'tc', 'migrate'])
42
+ else await execa('tc', ['migrate'])
34
43
  }
35
44
 
36
45
  exports.description = 'upgrade toolchains and migrate'
package/util/execa.cjs CHANGED
@@ -1,7 +1,6 @@
1
1
  const Path = require('path')
2
2
  const execa = require('execa')
3
3
  const chalk = require('chalk')
4
- const { projectDir, toolchainPackages } = require('./findUps.cjs')
5
4
 
6
5
  function formatArg(arg) {
7
6
  if (/^[-_a-z0-9=./]+$/i.test(arg)) return arg
@@ -15,6 +14,13 @@ function extractCommand(command) {
15
14
  }
16
15
 
17
16
  function getExecaArgs(command, args, options, ...rest) {
17
+ let projectDir, toolchainPackages
18
+ try {
19
+ ;({ projectDir, toolchainPackages } = require('./findUps.cjs'))
20
+ } catch (error) {
21
+ projectDir = process.cwd()
22
+ toolchainPackages = []
23
+ }
18
24
  if (args instanceof Object && !Array.isArray(args)) {
19
25
  options = args
20
26
  args = []
package/util/findUps.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  const findUp = require('find-up')
2
2
  const Path = require('path')
3
3
  const fs = require('fs-extra')
4
+ const glob = require('glob')
4
5
  const merge = require('./merge.cjs')
5
6
  const once = require('./once.cjs')
6
7
  const { name } = require('../package.json')
@@ -59,6 +60,16 @@ exports.monorepoPackageJson = monorepoPackageJsonFile
59
60
  ? fs.readJsonSync(monorepoPackageJsonFile)
60
61
  : undefined
61
62
 
63
+ exports.monorepoSubpackageJsonFiles = pnpmWorkspace
64
+ ? [
65
+ ...new Set(
66
+ pnpmWorkspace.packages.flatMap((p) =>
67
+ glob.sync(Path.join(p, 'package.json'), { cwd: monorepoProjectDir })
68
+ )
69
+ ),
70
+ ].map((f) => Path.resolve(monorepoProjectDir, f))
71
+ : undefined
72
+
62
73
  const findGitDir = once(function findGitDir(cwd = process.cwd()) {
63
74
  let stopAt = Path.dirname(monorepoProjectDir || projectDir)
64
75
  if (stopAt === '/') stopAt = undefined
@@ -1,8 +1,8 @@
1
1
  const Path = require('path')
2
2
  const sortPlugins = require('./sortPlugins.cjs')
3
- const { projectDir, toolchainPackages } = require('./findUps.cjs')
4
3
 
5
4
  function getPlugins(name) {
5
+ const { projectDir, toolchainPackages } = require('./findUps.cjs')
6
6
  const plugins = {}
7
7
  for (const pkg of toolchainPackages) {
8
8
  let path
@@ -0,0 +1,11 @@
1
+ const { parse: parseUrl } = require('url')
2
+
3
+ const repoRegExp = new RegExp('^/(.+?)/([^/.]+)')
4
+
5
+ module.exports = function parseRepositoryUrl(url) {
6
+ const parsed = parseUrl(url)
7
+ const match = repoRegExp.exec(parsed.path || '')
8
+ if (!match) throw new Error(`unsupported source repository url: ${url}`)
9
+ const [organization, repo] = match.slice(1)
10
+ return { ...parsed, organization, repo }
11
+ }