-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage2toml.js
73 lines (63 loc) · 1.87 KB
/
package2toml.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
const fs = require('fs')
const url = require('url')
const path = require('path')
const { promisify } = require('util')
const execAsync = promisify(require('child_process').exec)
const readFileAsync = promisify(fs.readFile)
const writeFileAsync = promisify(fs.writeFile)
async function read (src) {
const pkgFile = path.join(src, 'package.json')
let data = {}
try {
const fd = await readFileAsync(pkgFile, 'utf8')
data = JSON.parse(fd)
return data
} catch (err) {
console.log(`Failed to read or parse package.json at ${pkgFile}`)
console.log(err)
throw err
}
}
function formatTOML (data, user, registry) {
const toml = `
name = "${user}@${registry}/${data.name}"
version = "${data.version}"
[dependencies]
${Object.keys(data.dependencies).map(k => `"legacy@${registry}/${k}" = "${data.dependencies[k]}"`).join('\n')}
`
return toml
}
async function write (src, data) {
const tomlFile = path.join(src, 'Package.toml')
try {
await writeFileAsync(tomlFile, data, 'utf8')
} catch (err) {
console.log(`Failed to write Package.toml at ${tomlFile}`)
console.log(err)
throw err
}
}
async function getUser (registry) {
const reg = registry ? `https://${registry}` : null
const cmd = `ds whoami ${reg ? `--registry=${reg}` : ''}`
try {
const output = await execAsync(cmd)
return output.stdout.trim()
} catch (err) {
console.log('Could not get username from `ds whoami` and no --user was specified')
console.log(err)
throw err
}
}
async function main (opts) {
const srcDir = opts.srcDir || process.cwd()
let registry = opts.registry || 'registry.entropic.dev'
if (registry.includes('/')) {
registry = url.parse(registry).hostname
}
const user = opts.user || await getUser(registry)
const data = await read(srcDir)
const toml = formatTOML(data, user, registry)
await write(srcDir, toml)
}
module.exports = main