itns-sidechain/scripts/gen-hsclient.js
2023-01-16 23:49:05 +04:00

200 lines
4.1 KiB
JavaScript
Executable file

#!/usr/bin/env node
'use strict';
const assert = require('assert');
const path = require('path');
const fs = require('bfile');
const os = require('os');
const util = require('util');
const cp = require('child_process');
const exec = util.promisify(cp.exec);
const ROOT = path.dirname(__dirname);
const HSD_PKG = require(path.join(ROOT, 'package.json'));
const REMOTE = 'git@github.com:handshake-org/hs-client.git';
const INIT_HS_CLIENT_PKG = {
name: "hs-client",
description: "HSD node and wallet client",
keywords: [
"http",
"request",
"socket.io",
"websockets"
],
main: "./lib/client/index.js",
bin: {
"hsd-cli": "./bin/hsd-cli",
"hsd-rpc": "./bin/hsd-rpc",
"hsw-cli": "./bin/hsw-cli",
"hsw-rpc": "./bin/hsw-rpc"
},
engines: {
node: ">=8.0.0"
}
}
const INHERIT_PROPERTIES = [
"version",
"license",
"repository",
"homepage",
"bugs",
"author"
];
const DEPENDENCIES = [
"bcfg",
"bcurl",
"bsert"
];
const COPY_FILES = [
".npmignore",
".gitignore",
"bin/hsd-cli",
"bin/hsw-cli",
"bin/hsd-rpc",
"bin/hsw-rpc",
"lib/client/index.js",
"lib/client/wallet.js",
"lib/client/node.js",
];
const README = `
hs-client
=========
Autogenerated from https://github.com/handshake-org/hsd.
REST and RPC client for handshake.
## Usage
\`\`\` js
const {NodeClient, WalletClient} = require('hs-client');
\`\`\`
`;
async function ensureDir() {
if (!await fs.exists(ROOT))
throw new Error(`${ROOT} does not exist.`);
const {HS_CLIENT_DIR} = process.env;
let HS_PKG = HS_CLIENT_DIR ? path.resolve(HS_CLIENT_DIR) : null;
if (!HS_PKG)
HS_PKG = path.join(os.tmpdir(), `hs-client`);
if (HS_PKG.startsWith(ROOT))
throw new Error(`hs-client needs to be outside of the hsd. ${HS_PKG}`);
console.log('hs-client directory: ', HS_PKG);
if (await fs.exists(HS_PKG)) {
throw new Error(
`Directory ${HS_PKG} already exists.`
+ ' Please remove to proceed or choose different directory.'
);
}
await fs.mkdir(HS_PKG);
return HS_PKG;
}
async function setupPackageContent(dir) {
for (const file of COPY_FILES) {
const src = path.join(ROOT, file);
const dst = path.join(dir, file);
const dstDir = path.dirname(dst);
if (!await fs.exists(dstDir))
await fs.mkdirp(dstDir);
await fs.copy(src, dst);
}
const hsClientPkg = {
...INIT_HS_CLIENT_PKG,
};
for (const name of INHERIT_PROPERTIES) {
assert(HSD_PKG[name]);
hsClientPkg[name] = HSD_PKG[name];
}
hsClientPkg.dependencies = {};
for (const dep of DEPENDENCIES) {
assert(HSD_PKG.dependencies[dep], `Dependency "${dep}" not found for hsd.`);
hsClientPkg.dependencies[dep] = HSD_PKG.dependencies[dep];
}
await fs.writeJSON(path.join(dir, 'package.json'), hsClientPkg);
await fs.writeFile(path.join(dir, 'README.md'), README);
return hsClientPkg;
}
async function setupGit(dir, version) {
console.log('Setting up git: ', dir);
const commands = [
'git init -b master',
`git remote add origin ${REMOTE}`,
'git add .',
`git commit --gpg-sign -m "v${version}"`,
`git tag --sign v${version} -m "v${version}"`
];
const manualCommands = [
'git push -f origin master',
`git push -f origin v${version}`
];
for (const cmd of [...commands]) {
console.log(`executing: ${cmd} in ${dir}.`);
try {
console.log(await execCmd(cmd, dir));
} catch (e) {
console.log(`Failed to execute: ${cmd}.`);
console.log(e.message);
console.log(' You can proceed manually');
break;
}
commands.shift();
}
for (const command of [...commands, ...manualCommands])
console.log(`Needs exec: ${command}`);
}
(async () => {
const HS_PKG = await ensureDir();
const pkg = await setupPackageContent(HS_PKG);
await setupGit(HS_PKG, pkg.version);
console.log('Needs: npm publish');
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});
async function execCmd(cmd, cwd, timeout = 2000) {
assert(cwd, 'CWD is required.');
const {stdout, stderr} = await exec(cmd, {
cwd,
timeout
});
if (stderr.length != 0)
throw new Error(stderr);
return stdout;
}