napi-rs/scripts/napi.js

91 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-04-28 17:26:38 +09:00
#!/usr/bin/env node
const parseArgs = require('minimist')
const path = require('path')
const os = require('os')
2020-02-19 00:05:13 +09:00
const toml = require('toml')
const fs = require('fs')
2018-04-28 17:26:38 +09:00
2020-02-19 00:05:13 +09:00
let tomlContentString
let tomlContent
let moduleName
try {
tomlContentString = fs.readFileSync(
path.join(process.cwd(), 'Cargo.toml'),
'utf-8',
)
2020-02-19 00:05:13 +09:00
} catch {
throw new TypeError('Can not find Cargo.toml in process.cwd')
}
try {
tomlContent = toml.parse(tomlContentString)
} catch {
throw new TypeError('Can not parse the Cargo.toml')
}
if (tomlContent.package && tomlContent.package.name) {
moduleName = tomlContent.package.name.replace(/-/g, '_')
} else {
throw new TypeError('No package.name field in Cargo.toml')
2018-04-28 17:26:38 +09:00
}
const argv = parseArgs(process.argv.slice(2), {
2020-04-27 01:11:19 +09:00
boolean: ['release', 'platform'],
2018-04-28 17:26:38 +09:00
})
const platform = os.platform()
2020-02-19 00:05:13 +09:00
let libExt
let dylibName = moduleName
2018-04-28 17:26:38 +09:00
// Platform based massaging for build commands
switch (platform) {
case 'darwin':
libExt = '.dylib'
2020-02-19 00:05:13 +09:00
dylibName = `lib${moduleName}`
2018-04-28 17:26:38 +09:00
break
case 'win32':
libExt = '.dll'
break
case 'linux':
2020-02-19 00:05:13 +09:00
dylibName = `lib${moduleName}`
2018-04-28 17:26:38 +09:00
libExt = '.so'
break
default:
console.error(
'Operating system not currently supported or recognized by the build script',
)
process.exit(1)
}
2020-02-19 00:05:13 +09:00
const targetDir = argv.release ? 'release' : 'debug'
2020-04-27 01:11:19 +09:00
const platformName = argv.platform ? `.${platform}` : ''
2020-02-19 00:05:13 +09:00
let subcommand =
argv._[0] ||
path.join('target', targetDir, `${moduleName}${platformName}.node`)
2020-04-27 01:11:19 +09:00
const parsedDist = path.parse(subcommand)
2020-02-19 00:05:13 +09:00
if (!parsedDist.name || parsedDist.name === '.') {
subcommand = moduleName
2018-04-28 17:26:38 +09:00
}
2020-02-19 00:05:13 +09:00
if (!parsedDist.ext) {
2020-04-27 01:11:19 +09:00
subcommand = `${subcommand}${platformName}.node`
2020-02-19 00:05:13 +09:00
}
const pos = __dirname.indexOf('node_modules')
const dylibContent = fs.readFileSync(
path.join(
__dirname.substring(0, pos),
'target',
targetDir,
`${dylibName}${libExt}`,
),
)
2020-02-19 00:05:13 +09:00
fs.writeFileSync(subcommand, dylibContent)