forked from effector/effector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postinstall.js
262 lines (239 loc) · 6.31 KB
/
postinstall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env node
// @flow
const fs = require('fs-extra')
const flowgen = require('flowgen')
const execa = require('execa')
const pkg = require('./package.json')
/*::
type Package = string
type Lockfile = {|
version: number,
packages: {
flowgen: {[key: string]: Package},
custom: {[key: string]: Package},
flowTyped: {[key: string]: Package},
stub: {[key: string]: Package},
builtin: {[key: string]: Package}
}
|}
*/
function normalizePattern(
pattern /*: string*/,
) /*: {
hasVersion: boolean,
name: string,
range: string,
}*/ {
let hasVersion = false
let range = 'latest'
let name = pattern
// if we're a scope then remove the @ and add it back later
let isScoped = false
if (name[0] === '@') {
isScoped = true
name = name.slice(1)
}
// take first part as the name
const parts = name.split('@')
if (parts.length > 1) {
name = parts.shift()
range = parts.join('@')
if (range) {
hasVersion = true
} else {
range = '*'
}
}
// add back @ scope suffix
if (isScoped) {
name = `@${name}`
}
return {name, range, hasVersion}
}
const defaultLockfile = {
version: 2,
packages: {
flowgen: {},
custom: {},
flowTyped: {},
stub: {},
builtin: {},
},
}
const lockfileData = fs.existsSync('./flow-typed/flow.lock')
const lockfile /*: Lockfile*/ = lockfileData
? fs.readJSONSync('./flow-typed/flow.lock', {
encoding: 'utf8',
})
: defaultLockfile
function setLockfilePackage(
newType /*: 'flowgen' | 'custom' | 'flowTyped' | 'stub' | 'builtin' */,
name /*: string */,
path /*: string */,
) {
const types = ['flowgen', 'custom', 'flowTyped', 'stub', 'builtin']
for (const type of types) {
if (!lockfile.packages[type]) lockfile.packages[type] = {}
if (type === newType) {
lockfile.packages[type][name] = path
continue
}
if (lockfile.packages[type]) delete lockfile.packages[type][name]
}
}
function hasLockfilePackage(name /*:string*/) {
let hasPackage = false
const types = ['flowgen', 'custom', 'flowTyped', 'stub', 'builtin']
for (const type of types) {
if (typeof lockfile.packages[type][name] !== 'undefined') {
hasPackage = true
}
}
return hasPackage
}
function fileName(input) {
if (input.startsWith('@types')) {
const name = input.replace('@types/', '')
if (name.includes('__')) {
const split = name.split('__')
const scope = `@${split[0]}`
const pkg = split[1]
return `${scope}/${pkg}`
}
return name
}
return input
}
async function getTypescriptFile(packageName) {
if (hasLockfilePackage(fileName(packageName))) {
console.log(`Package ${packageName} already exists.`)
return
} else {
setLockfilePackage('custom', fileName(packageName), '')
}
const isTypes = packageName.startsWith('@types')
let typesPackageName
if (isTypes) {
typesPackageName = packageName
} else {
typesPackageName = packageName.startsWith('@')
? `@types/${packageName.replace('@', '').replace('/', '__')}`
: `@types/${packageName}`
}
let typescriptFile
let hasFlowBuiltin = false
//$off
const json = require(`${packageName}/package.json`)
try {
const flowTypingsPath = require.resolve(`${packageName}/index.js.flow`)
if (flowTypingsPath) {
hasFlowBuiltin = true
setLockfilePackage('builtin', fileName(packageName), '')
}
//console.log(flowTypingsPath)
} catch {}
const typingsPath = json.typings || json.types
//console.log(process.env.npm_config_user_agent, process.env.PWD)
console.log(`+ ${packageName}`)
if (hasFlowBuiltin) return
try {
const {stdout} = await execa('flow-typed', [
'install',
fileName(packageName),
])
setLockfilePackage('flowTyped', fileName(packageName), '')
//console.log(stdout)
return
} catch (err) {
if (err.failed) {
//console.error(err.stdout)
}
}
try {
const path = require.resolve(
`${packageName}/${typingsPath || 'index.d.ts'}`,
)
const name = fileName(packageName)
//console.log(packageName, name, path)
const file = await fs.readFile(path, 'utf8')
const hasHeader =
file.includes(`declare module '${name}'`) ||
file.includes(`declare module "${name}"`)
const template = hasHeader
? file
: `
declare module '${name}' {
${file}
};
`
try {
const code = flowgen.beautify(
flowgen.compiler.compileDefinitionString(template),
)
await fs.ensureFile(`flow-typed/flowgen/${name}_v${json.version}.js`)
await fs.writeFile(
`flow-typed/flowgen/${name}_v${json.version}.js`,
`/**
* This is an autogenerated libdef stub for:
*
* '${name}'
*
* Fill this stub out by fixing all the errors.
*
* Once filled out, we encourage you to share your work with the
* community by sending a pull request to:
* https://github.com/flowtype/flow-typed
*/
${code}
`,
)
setLockfilePackage(
'flowgen',
fileName(packageName),
`flow-typed/flowgen/${name}_v${json.version}.js`,
)
} catch (err) {
//console.log("Can't convert package", err)
await execa('flow-typed', ['create-stub', name])
setLockfilePackage(
'stub',
fileName(packageName),
`flow-typed/npm/${name}_vx.x.x.js`,
)
}
return ''
} catch (err) {
//console.error(err)
//console.log(packageName, 'no index.d.ts')
if (packageName === typesPackageName) return
}
try {
require(`${typesPackageName}/package.json`)
return getTypescriptFile(typesPackageName)
} catch (err) {
//console.error(err)
//console.log(typesPackageName, 'no types')
try {
await execa('yarn', ['add', `${typesPackageName}`, '-D'])
return getTypescriptFile(typesPackageName)
} catch {
//console.error(`package ${typesPackageName} not found`)
}
}
}
//$off
const packagePattern = JSON.parse(process.env.npm_config_argv).original[1]
if (!packagePattern) {
async function main() {
const deps = {...pkg.dependencies, ...pkg.devDependencies}
for (const key in deps) {
await getTypescriptFile(key)
}
await fs.writeJSON('./flow-typed/flow.lock', lockfile, {spaces: 2})
process.exit(0)
}
main()
} else {
const normalizedPattern = normalizePattern(packagePattern)
getTypescriptFile(normalizedPattern.name).then(console.log)
}