Skip to content

Commit

Permalink
Add the platform, arch, and node_version in the teraslice cli asset b…
Browse files Browse the repository at this point in the history
…uild
  • Loading branch information
peterdemartini committed Jun 19, 2020
1 parent 8a48b0c commit eca9ae0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/teraslice-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"chalk": "^4.1.0",
"cli-table3": "^0.6.0",
"easy-table": "^1.1.1",
"execa": "^4.0.2",
"fs-extra": "^9.0.1",
"got": "^11.3.0",
"js-yaml": "^3.14.0",
Expand Down
43 changes: 22 additions & 21 deletions packages/teraslice-cli/src/helpers/asset-src.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { spawnSync } from 'child_process';
import execa from 'execa';
import fs from 'fs-extra';
import archiver from 'archiver';
import path from 'path';
import tmp from 'tmp';
import { toInteger } from '@terascope/utils';
import { getPackage } from '../helpers/utils';

interface ZipResults {
Expand Down Expand Up @@ -39,11 +40,11 @@ export default class AssetSrc {
}

/** @returns {string} Path to the output drectory for the finished asset zipfile */
get buildDir() {
get buildDir(): string {
return path.join(this.srcDir, 'build');
}

get zipFileName() {
get zipFileName(): string {
const nodeVersion = process.version.split('.')[0].substr(1);
return `${this.name}-v${this.version}-node-${nodeVersion}-${process.platform}-${process.arch}.zip`;
}
Expand All @@ -56,51 +57,51 @@ export default class AssetSrc {
* @param {string} dir - Path to directory containing package.json
* @param {Array} yarnArgs - Array of arguments or options to be passed to yarn command
*/
private _yarnCmd(dir: string, yarnArgs: string[]) {
const yarn = spawnSync('yarn', yarnArgs, { cwd: dir });

if (yarn.status !== 0) {
throw new Error(
`yarn command exited with non-zero status: ${yarn.status}\n
yarn stdout:\n${yarn.stdout}\n
yarn stderr:\n${yarn.stderr}`
);
}
return yarn;
private async _yarnCmd(dir: string, yarnArgs: string[]) {
return execa('yarn', yarnArgs, { cwd: dir });
}

async build() {
async build(): Promise<string> {
let zipOutput;
const outputFileName = path.join(this.buildDir, this.zipFileName);

try {
// make sure the build dir exists in the srcDir directory
fs.ensureDirSync(this.buildDir);
await fs.ensureDir(this.buildDir);
} catch (err) {
throw new Error(`Failed to create directory ${this.buildDir}: ${err}`);
}
// make temp dir
const tmpDir = tmp.dirSync();

// copy entire asset dir (srcDir) to tempdir
fs.copySync(this.srcDir, tmpDir.name);
await fs.copy(this.srcDir, tmpDir.name);

const assetJSON = await fs.readJSON(path.join(tmpDir.name, 'asset', 'asset.json'));
assetJSON.node_version = toInteger(process.version.split('.')[0].substr(1));
assetJSON.platform = process.platform;
assetJSON.arch = process.arch;

await fs.writeJSON(path.join(tmpDir.name, 'asset', 'asset.json'), assetJSON, {
spaces: 4,
});

// remove srcDir/asset/node_modules
fs.removeSync(path.join(tmpDir.name, 'asset', 'node_modules'));
await fs.remove(path.join(tmpDir.name, 'asset', 'node_modules'));

// run yarn --cwd srcDir/asset --prod --silent --no-progress
this._yarnCmd(path.join(tmpDir.name, 'asset'), ['--prod', '--no-progress']);
await this._yarnCmd(path.join(tmpDir.name, 'asset'), ['--prod', '--no-progress']);

// run yarn --cwd srcDir --prod --silent --no-progress asset:build
if (this.packageJson?.scripts && this.packageJson.scripts['asset:build']) {
this._yarnCmd(tmpDir.name, ['run', 'asset:build']);
await this._yarnCmd(tmpDir.name, ['run', 'asset:build']);
}

try {
// create zipfile
zipOutput = await AssetSrc.zip(path.join(tmpDir.name, 'asset'), outputFileName);
// remove temp directory
fs.removeSync(tmpDir.name);
await fs.remove(tmpDir.name);
} catch (err) {
throw new Error(`Error creating asset zipfile: ${err}`);
}
Expand Down

0 comments on commit eca9ae0

Please sign in to comment.