Skip to content

Commit

Permalink
build: use execa for the packager
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <[email protected]>
  • Loading branch information
Akos Kitta authored and kittaakos committed May 9, 2023
1 parent 33ab2a6 commit 36e2092
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 301 deletions.
24 changes: 12 additions & 12 deletions electron/packager/config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//@ts-check

const fs = require('fs');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const semver = require('semver');
const merge = require('deepmerge');
const dateFormat = require('dateformat');
const { isNightly, isRelease, git } = require('./utils');

function artifactName() {
async function artifactName() {
const { platform, arch } = process;
const id = (() => {
const id = await (() => {
if (isRelease) {
return getVersion();
} else if (isNightly) {
Expand Down Expand Up @@ -69,8 +69,8 @@ function electronPlatform() {
}
}

function getVersion() {
const repositoryRootPath = git('rev-parse --show-toplevel');
async function getVersion() {
const repositoryRootPath = await git(['rev-parse', '--show-toplevel']);
let version = JSON.parse(
fs.readFileSync(path.join(repositoryRootPath, 'package.json'), {
encoding: 'utf8',
Expand All @@ -85,7 +85,7 @@ function getVersion() {
if (isNightly) {
version = `${version}-nightly-${timestamp()}`;
} else {
version = `${version}-snapshot-${currentCommitish()}`;
version = `${version}-snapshot-${await currentCommitish()}`;
}
if (!semver.valid(version)) {
throw new Error(`Invalid patched version: '${version}'.`);
Expand All @@ -109,18 +109,18 @@ function timestamp() {
return dateFormat(new Date(), 'yyyymmdd');
}

function currentCommitish() {
return git('rev-parse --short HEAD');
async function currentCommitish() {
return git(['rev-parse', '--short', 'HEAD']);
}

// function currentBranch() {
// return git('rev-parse --abbrev-ref HEAD');
// }

function generateTemplate(buildDate) {
async function generateTemplate(buildDate) {
// do `export PUBLISH=true yarn package` if you want to mimic CI build locally.
// const electronPublish = release || (isCI && currentBranch() === 'main') || process.env.PUBLISH === 'true';
const version = getVersion();
const version = await getVersion();
const productName = 'Arduino IDE';
const name = 'arduino-ide';
const updateChannel = getChannel();
Expand All @@ -139,7 +139,7 @@ function generateTemplate(buildDate) {
productName,
appId: 'cc.arduino.IDE2',
[electronPlatform()]: {
artifactName: artifactName(),
artifactName: await artifactName(),
},
},
};
Expand Down
108 changes: 68 additions & 40 deletions electron/packager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
throw reason;
});

const fs = require('fs');
const join = require('path').join;
const fs = require('node:fs');
const join = require('node:path').join;
const shell = require('shelljs');
const { echo, cp, mkdir, mv, rm } = shell;
shell.config.fatal = true;
Expand All @@ -32,7 +32,7 @@
// https://github.com/shelljs/shelljs/issues/1024#issuecomment-1001552543
shell.env.NODE_OPTIONS = '--max_old_space_size=4096'; // Increase heap size for the CI
shell.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = 'true'; // Skip download and avoid `ERROR: Failed to download Chromium`.
const template = require('./config').generateTemplate(
const template = await require('./config').generateTemplate(
new Date().toISOString()
);
const utils = require('./utils');
Expand Down Expand Up @@ -74,12 +74,14 @@
// Clean up the `./electron/build/resources` folder with Git.
// To avoid file duplication between bundled app and dev mode, some files are copied from `./electron-app` to `./electron/build` folder.
const foldersToSyncFromDev = ['resources'];
foldersToSyncFromDev.forEach((filename) =>
shell.exec(
`git -C ${join(repoRoot, 'electron', 'build', filename)} clean -ffxdq`,
{
async: false,
}
await Promise.all(
foldersToSyncFromDev.map((filename) =>
exec('git', [
'-C',
join(repoRoot, 'electron', 'build', filename),
'clean',
'-ffxdq',
])
)
);

Expand All @@ -104,15 +106,20 @@
// Build and test the extensions |
//-------------------------------+
for (const extension of extensions) {
exec(
`yarn --network-timeout 1000000 --cwd ${join(repoRoot, extension)}`,
await exec(
'yarn',
['--network-timeout', '1000000', '--cwd', join(repoRoot, extension)],
`Building and testing ${extension}`
);
exec(
`yarn --network-timeout 1000000 --cwd ${join(
repoRoot,
extension
)} test:slow`,
await exec(
'yarn',
[
'--network-timeout',
'1000000',
'--cwd',
join(repoRoot, extension),
'test:slow',
],
`Executing slow tests ${extension}`
);
}
Expand Down Expand Up @@ -142,11 +149,21 @@
for (const extension of extensions) {
const packageJsonPath = join(repoRoot, extension, 'package.json');
const versionToRestore = readJson(packageJsonPath).version;
exec(
`yarn --network-timeout 1000000 --cwd ${join(
repoRoot,
extension
)} publish --ignore-scripts --new-version ${version} --no-git-tag-version --registry http://localhost:4873`,
await exec(
'yarn',
[
'--network-timeout',
'1000000',
'--cwd',
join(repoRoot, extension),
'publish',
'--ignore-scripts',
'--new-version',
version,
'--no-git-tag-version',
'--registry',
'http://localhost:4873',
],
`Publishing ${extension}@${version} to the private npm registry`
);
// Publishing will change the version number, this should be reverted up after the build.
Expand Down Expand Up @@ -250,20 +267,26 @@ ${fs
//-------------------------------------------------------------------------------------------+
// Install all private and public dependencies for the electron application and build Theia. |
//-------------------------------------------------------------------------------------------+
exec(
`yarn --network-timeout 1000000 --cwd ${join(
repoRoot,
'electron',
'build'
)} --registry http://localhost:4873`,
await exec(
'yarn',
[
'--network-timeout',
'1000000',
'--cwd',
join(repoRoot, 'electron', 'build'),
'--registry',
'http://localhost:4873',
],
'Installing dependencies'
);
exec(
`yarn --cwd ${join(repoRoot, 'electron', 'build')} build`,
await exec(
'yarn',
['--cwd', join(repoRoot, 'electron', 'build'), 'build'],
`Building the ${productName} application`
);
exec(
`yarn --cwd ${join(repoRoot, 'electron', 'build')} rebuild`,
await exec(
'yarn',
['--cwd', join(repoRoot, 'electron', 'build'), 'rebuild'],
'Rebuilding native dependencies'
);

Expand All @@ -284,8 +307,9 @@ ${fs
//-----------------------------------+
// Package the electron application. |
//-----------------------------------+
exec(
`yarn --cwd ${join(repoRoot, 'electron', 'build')} package`,
await exec(
'yarn',
['--cwd', join(repoRoot, 'electron', 'build'), 'package'],
`Packaging the ${productName} application`
);

Expand Down Expand Up @@ -317,15 +341,19 @@ ${fs
//--------+
// Utils. |
//--------+
function exec(command, toEcho) {
/**
* @param {string} command
* @param {readonly string[]} args
*/
async function exec(command, args, toEcho = '') {
if (toEcho) {
echo(`⏱️ >>> ${toEcho}...`);
}
const { stdout } = shell.exec(command);
const stdout = await utils.exec(command, args);
if (toEcho) {
echo(`👌 <<< ${toEcho}.`);
}
return stdout;
return stdout.trim();
}

async function copyFilesToBuildArtifacts() {
Expand Down Expand Up @@ -437,13 +465,13 @@ ${fs
}

/**
* @param {import('fs').PathLike} file
* @param {import('node:fs').PathLike} file
* @param {string|undefined} [algorithm="sha512"]
* @param {BufferEncoding|undefined} [encoding="base64"]
* @param {object|undefined} [options]
*/
function hashFile(file, algorithm = 'sha512', encoding = 'base64', options) {
const crypto = require('crypto');
const crypto = require('node:crypto');
return new Promise((resolve, reject) => {
const hash = crypto.createHash(algorithm);
hash.on('error', reject).setEncoding(encoding);
Expand Down Expand Up @@ -500,11 +528,11 @@ ${fs

/**
* @param {string} configPath
* @return {Promise<import('child_process').ChildProcess>}
* @return {Promise<import('node:child_process').ChildProcess>}
*/
function startNpmRegistry(configPath) {
return new Promise((resolve, reject) => {
const fork = require('child_process').fork(
const fork = require('node:child_process').fork(
require.resolve('verdaccio/bin/verdaccio'),
['-c', configPath]
);
Expand Down
7 changes: 2 additions & 5 deletions electron/packager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
"author": "Arduino SA",
"license": "AGPL-3.0-or-later",
"dependencies": {
"@types/file-type": "^10.9.1",
"@types/temp": "^0.8.32",
"7zip-min": "^1.1.1",
"chai": "^4.2.0",
"crypto": "^1.0.1",
"dateformat": "^3.0.3",
"deepmerge": "2.01",
"execa": "^7.1.1",
"file-type": "^14.1.4",
"glob": "^7.1.6",
"is-ci": "^2.0.0",
Expand All @@ -29,8 +27,7 @@
"sinon": "^9.0.1",
"temp": "^0.9.1",
"verdaccio": "6-next",
"yaml": "^1.10.2",
"yargs": "^12.0.5"
"yaml": "^1.10.2"
},
"engines": {
"node": ">=16.14.0 <17"
Expand Down
4 changes: 2 additions & 2 deletions electron/packager/test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const expect = require('chai').expect;
const track = require('temp').track();
const unpack = require('../utils').unpack;
Expand Down
32 changes: 24 additions & 8 deletions electron/packager/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,21 @@ const isElectronPublish = false; // TODO: support auto-updates
const isNightly = process.env.IS_NIGHTLY === 'true';
const isRelease = process.env.IS_RELEASE === 'true';

function git(command) {
/**
* @param {readonly string[]} args
*/
async function git(args) {
try {
const gitPath = shell.which('git');
const git = shell.which('git');
const error = shell.error();
if (error) {
throw new Error(error);
}
const { stderr, stdout } = shell.exec(`"${gitPath}" ${command}`, {
silent: true,
});
if (stderr) {
throw new Error(stderr.toString().trim());
if (!git) {
throw new Error("Could not find 'git' on the $PATH");
}
return stdout.toString().trim();
const stdout = await exec(git.toString(), args);
return stdout;
} catch (e) {
throw e;
}
Expand Down Expand Up @@ -168,6 +169,20 @@ function getChannelFile(platform) {
);
}

/**
* @param {string} command
* @param {readonly string[]} args
*/
async function exec(command, args) {
const execa = await import('execa');
const promise = execa.execa(command, args);
if (promise.pipeStdout) {
promise.pipeStdout(process.stdout);
}
const { stdout } = await promise;
return stdout;
}

module.exports = {
adjustArchiveStructure,
isZip,
Expand All @@ -177,4 +192,5 @@ module.exports = {
isElectronPublish,
git,
getChannelFile,
exec,
};
Loading

0 comments on commit 36e2092

Please sign in to comment.