Skip to content

Commit c6ce1fc

Browse files
chantastickentcdodds
authored andcommitted
migrate: convert scripts to type module
1 parent 40ce72c commit c6ce1fc

4 files changed

Lines changed: 42 additions & 31 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"githubRoot": "https://github.com/kentcdodds/react-hooks/blob/main",
1010
"root": true
1111
},
12+
"type": "module",
1213
"keywords": [],
1314
"homepage": "https://react-hooks-next.netlify.app/",
1415
"license": "GPL-3.0-only",

scripts/pre-commit.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
var spawnSync = require('child_process').spawnSync
2-
const {username} = require('os').userInfo()
1+
import {spawnSync} from 'child_process'
2+
import os from 'os'
33

4+
const {username} = os.userInfo()
45
if (username === 'kentcdodds') {
5-
const result = spawnSync('npm run validate', {stdio: 'inherit', shell: true})
6+
const result = spawnSync('npm run validate', {
7+
stdio: 'inherit',
8+
shell: true,
9+
})
610

711
if (result.status !== 0) {
812
process.exit(result.status)

scripts/pre-push.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
import fs from 'fs'
2+
import os from 'os'
3+
import path from 'path'
4+
15
try {
2-
const {username} = require('os').userInfo()
6+
const {username} = os.userInfo()
37
const {
48
repository: {url: repoUrl},
5-
} = require('../package.json')
9+
} = fs.readFileSync(path.join(process.cwd(), 'package.json'))
610

711
const remote = process.env.HUSKY_GIT_PARAMS.split(' ')[1]
812
const repoName = repoUrl.match(/(?:.(?!\/))+\.git$/)[0]
913
if (
10-
!['kentcdodds', 'chan'].includes(username) &&
14+
['kentcdodds', 'chan'].includes(username) &&
1115
remote.includes(`kentcdodds${repoName}`)
1216
) {
1317
console.log(

scripts/setup.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
1-
var spawnSync = require('child_process').spawnSync
1+
import { spawnSync } from 'child_process'
22

33
var styles = {
4-
// got these from playing around with what I found from:
5-
// https://github.com/istanbuljs/istanbuljs/blob/0f328fd0896417ccb2085f4b7888dd8e167ba3fa/packages/istanbul-lib-report/lib/file-writer.js#L84-L96
6-
// they're the best I could find that works well for light or dark terminals
7-
success: {open: '\u001b[32;1m', close: '\u001b[0m'},
8-
danger: {open: '\u001b[31;1m', close: '\u001b[0m'},
9-
info: {open: '\u001b[36;1m', close: '\u001b[0m'},
10-
subtitle: {open: '\u001b[2;1m', close: '\u001b[0m'},
4+
// got these from playing around with what I found from:
5+
// https://github.com/istanbuljs/istanbuljs/blob/0f328fd0896417ccb2085f4b7888dd8e167ba3fa/packages/istanbul-lib-report/lib/file-writer.js#L84-L96
6+
// they're the best I could find that works well for light or dark terminals
7+
success: { open: '\u001b[32;1m', close: '\u001b[0m' },
8+
danger: { open: '\u001b[31;1m', close: '\u001b[0m' },
9+
info: { open: '\u001b[36;1m', close: '\u001b[0m' },
10+
subtitle: { open: '\u001b[2;1m', close: '\u001b[0m' },
1111
}
1212

1313
function color(modifier, string) {
14-
return styles[modifier].open + string + styles[modifier].close
14+
return styles[modifier].open + string + styles[modifier].close
1515
}
1616

1717
console.log(color('info', '▶️ Starting workshop setup...'))
1818

19-
var output = spawnSync('npm --version', {shell: true}).stdout.toString().trim()
19+
var output = spawnSync('npm --version', { shell: true })
20+
.stdout.toString()
21+
.trim()
2022
var outputParts = output.split('.')
2123
var major = Number(outputParts[0])
2224
var minor = Number(outputParts[1])
2325
if (major < 8 || (major === 8 && minor < 16)) {
24-
console.error(
25-
color(
26-
'danger',
27-
'🚨 npm version is ' +
28-
output +
29-
' which is out of date. Please install npm@8.16.0 or greater',
30-
),
31-
)
32-
throw new Error('npm version is out of date')
26+
console.error(
27+
color(
28+
'danger',
29+
'🚨 npm version is ' +
30+
output +
31+
' which is out of date. Please install npm@8.16.0 or greater',
32+
),
33+
)
34+
throw new Error('npm version is out of date')
3335
}
3436

3537
var command =
36-
'npx "https://gist.github.com/kentcdodds/bb452ffe53a5caa3600197e1d8005733" -q'
38+
'npx "https://gist.github.com/kentcdodds/bb452ffe53a5caa3600197e1d8005733" -q'
3739
console.log(
38-
color('subtitle', ' Running the following command: ' + command),
40+
color('subtitle', ' Running the following command: ' + command),
3941
)
4042

41-
var result = spawnSync(command, {stdio: 'inherit', shell: true})
43+
var result = spawnSync(command, { stdio: 'inherit', shell: true })
4244

4345
if (result.status === 0) {
44-
console.log(color('success', '✅ Workshop setup complete...'))
46+
console.log(color('success', '✅ Workshop setup complete...'))
4547
} else {
46-
process.exit(result.status)
48+
process.exit(result.status)
4749
}
4850

4951
/*
5052
eslint
5153
no-var: "off",
5254
"vars-on-top": "off",
53-
*/
55+
*/

0 commit comments

Comments
 (0)