@jcoreio/toolchain 4.0.0 → 4.1.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.0",
3
+ "version": "4.1.0",
4
4
  "description": "base JS build toolchain",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,200 @@
1
+ const path = require('path')
2
+ const execa = require('../util/execa.cjs')
3
+ const ChdirFs = require('../util/ChdirFs.cjs')
4
+ const pkgName = require('../package.json').name
5
+ const dedent = require('dedent-js')
6
+
7
+ async function create(args = []) {
8
+ const prompt = require('prompts')
9
+
10
+ const required = (s) => Boolean(s) || 'required'
11
+
12
+ let defaultAuthor
13
+ try {
14
+ defaultAuthor = (
15
+ await execa('git', ['config', 'user.name'], {
16
+ stdio: 'pipe',
17
+ maxBuffer: 1024,
18
+ encoding: 'utf8',
19
+ })
20
+ ).stdout.trim()
21
+ } catch (error) {
22
+ // ignore
23
+ }
24
+
25
+ const questions = [
26
+ {
27
+ type: 'text',
28
+ name: 'directory',
29
+ message: 'Destination directory:',
30
+ validate: required,
31
+ },
32
+ {
33
+ type: 'text',
34
+ name: 'name',
35
+ message: 'Package name:',
36
+ initial: (prev, { directory }) => path.basename(directory),
37
+ validate: required,
38
+ },
39
+ {
40
+ type: 'text',
41
+ name: 'description',
42
+ message: 'Package description:',
43
+ validate: required,
44
+ },
45
+ {
46
+ type: 'text',
47
+ name: 'author',
48
+ initial: defaultAuthor,
49
+ message: 'Package author:',
50
+ validate: required,
51
+ },
52
+ {
53
+ type: 'text',
54
+ name: 'keywords',
55
+ message: 'Package keywords:',
56
+ format: (text) => text.split(/\s*,\s*|\s+/g),
57
+ },
58
+ {
59
+ type: 'text',
60
+ name: 'organization',
61
+ initial: (prev, { name }) => {
62
+ const match = /^@(.*?)\//.exec(name)
63
+ if (match) return match[1]
64
+ },
65
+ message: 'GitHub organization:',
66
+ validate: required,
67
+ },
68
+ {
69
+ type: 'text',
70
+ name: 'repo',
71
+ message: 'GitHub repo:',
72
+ initial: (prev, { name }) => name.replace(/^@(.*?)\//, ''),
73
+ validate: required,
74
+ },
75
+ {
76
+ type: 'select',
77
+ name: 'license',
78
+ message: 'License',
79
+ choices: Object.entries(licenses).map(([value, { title }]) => ({
80
+ title,
81
+ value,
82
+ })),
83
+ },
84
+ {
85
+ type: 'text',
86
+ name: 'copyrightHolder',
87
+ message: 'Copyright holder:',
88
+ initial: (prev, { author }) => author,
89
+ validate: required,
90
+ },
91
+ {
92
+ name: 'ready',
93
+ type: 'confirm',
94
+ initial: true,
95
+ message: 'Ready to go?',
96
+ },
97
+ ]
98
+
99
+ let answers
100
+ do {
101
+ answers = await prompt(questions)
102
+ for (const question of questions) {
103
+ const { name, transformer } = question
104
+ if (name !== 'ready') {
105
+ const answer = answers[name]
106
+ question.initial = transformer ? transformer(answer) : answer
107
+ }
108
+ }
109
+ } while (!answers.ready)
110
+
111
+ const {
112
+ directory,
113
+ name,
114
+ description,
115
+ author,
116
+ keywords,
117
+ license,
118
+ copyrightHolder,
119
+ organization,
120
+ repo,
121
+ } = answers
122
+
123
+ const cwd = path.resolve(directory)
124
+
125
+ await require('fs-extra').mkdirs(path.join(cwd, 'src'))
126
+
127
+ const fs = ChdirFs(cwd)
128
+
129
+ const packageJson = {
130
+ name,
131
+ description,
132
+ repository: {
133
+ type: 'git',
134
+ url: `https://github.com/${organization}/${repo}.git`,
135
+ },
136
+ homepage: `https://github.com/${organization}/${repo}`,
137
+ bugs: {
138
+ url: `https://github.com/${organization}/${repo}`,
139
+ },
140
+ author,
141
+ license,
142
+ keywords,
143
+ }
144
+
145
+ await fs.writeJson('package.json', packageJson, { spaces: 2 })
146
+
147
+ const files = {
148
+ 'README.md': dedent`
149
+ # ${name}
150
+
151
+ ${description}
152
+
153
+ [![CircleCI](https://circleci.com/gh/${organization}/${repo}.svg?style=svg)](https://circleci.com/gh/${organization}/${repo})
154
+ [![Coverage Status](https://codecov.io/gh/${organization}/${repo}/branch/master/graph/badge.svg)](https://codecov.io/gh/${organization}/${repo})
155
+ [![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)
156
+ [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
157
+ [![npm version](https://badge.fury.io/js/${encodeURIComponent(
158
+ name
159
+ )}.svg)](https://badge.fury.io/js/${encodeURIComponent(name)})
160
+ `,
161
+ 'LICENSE.md': (
162
+ await fs.readFile(require.resolve(`./licenses/${license}.md`), 'utf8')
163
+ )
164
+ .replace(/<YEAR>/g, String(new Date().getFullYear()))
165
+ .replace(/<COPYRIGHT HOLDER>/g, copyrightHolder),
166
+ }
167
+ await Promise.all(
168
+ Object.entries(files).map(async ([name, content]) => {
169
+ const file = path.resolve(cwd, name)
170
+ await fs.writeFile(file, content, 'utf8')
171
+ // eslint-disable-next-line no-console
172
+ console.error(`wrote ${path.relative(process.cwd(), file)}`)
173
+ })
174
+ )
175
+
176
+ await execa('git', ['init'], { cwd })
177
+ await execa('git', ['remote', 'add', 'origin', packageJson.repository.url], {
178
+ cwd,
179
+ })
180
+ await execa(
181
+ 'pnpm',
182
+ ['add', '-D', '--prefer-offline', '--no-optional', pkgName],
183
+ { cwd }
184
+ )
185
+ await execa('pnpm', ['exec', 'tc', 'init'], { cwd })
186
+
187
+ await execa('git', ['add', '.'], { cwd })
188
+ await execa('git', ['commit', '-m', 'chore: initial commit from tc create'], {
189
+ cwd,
190
+ })
191
+ }
192
+
193
+ exports.description = 'create a new toolchain project'
194
+ exports.run = create
195
+
196
+ const licenses = {
197
+ MIT: {
198
+ title: 'The MIT License',
199
+ },
200
+ }
package/scripts/init.cjs CHANGED
@@ -69,6 +69,8 @@ async function init(args = []) {
69
69
  await execa('pnpm', [
70
70
  'add',
71
71
  '-D',
72
+ '--no-optional',
73
+ '--prefer-offline',
72
74
  ...(isMonorepoRoot ? ['-w'] : []),
73
75
  isTest ? '../packages/base' : `${name}@^${version}`,
74
76
  ...(isTest
@@ -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,6 +34,8 @@ async function migrate(args = []) {
34
34
  if (!args.includes('--config-only')) {
35
35
  await execa('pnpm', [
36
36
  'i',
37
+ '--no-optional',
38
+ '--prefer-offline',
37
39
  '--fix-lockfile',
38
40
  ...(isMonorepoRoot ? ['-w'] : []),
39
41
  ])
@@ -3,45 +3,56 @@
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
9
+ try {
10
+ ;({ toolchainConfig } = 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
+ ...getPluginsObjectSync('scripts'),
40
+ ...Object.fromEntries(
41
+ Object.entries(toolchainConfig.scripts || {}).map(([name, script]) => [
42
+ name,
43
+ typeof script === 'string'
44
+ ? {
45
+ run: (args = []) =>
46
+ execa([script, ...args].join(' '), { shell: true }),
47
+ description: script,
48
+ }
49
+ : script,
50
+ ])
51
+ ),
52
+ }
53
+ : {
54
+ create: require('./create.cjs'),
55
+ }
45
56
 
46
57
  exports.scripts = scripts
47
58
 
@@ -22,6 +22,8 @@ async function upgrade([version] = []) {
22
22
  await execa('pnpm', [
23
23
  'add',
24
24
  '-D',
25
+ '--no-optional',
26
+ '--prefer-offline',
25
27
  ...(isMonorepoRoot ? ['-w'] : []),
26
28
  isTest ? '../packages/base' : `${name}@^${version}`,
27
29
  ...(isTest
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 = []
@@ -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