Skip to content

Commit

Permalink
Move to ESM
Browse files Browse the repository at this point in the history
Change-type: major
  • Loading branch information
otaviojacobi committed Jan 1, 2025
1 parent 0900db5 commit 31fc12a
Show file tree
Hide file tree
Showing 205 changed files with 1,682 additions and 1,187 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

21 changes: 0 additions & 21 deletions .eslintrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion .mocharc-standalone.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const commonConfig = require('./.mocharc.js');
const commonConfig = require('./.mocharc.cjs');

module.exports = {
...commonConfig,
Expand Down
4 changes: 2 additions & 2 deletions .mocharc.js → .mocharc.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
reporter: 'spec',
require: 'ts-node/register/transpile-only',
file: './tests/config-tests',
require: 'tsx',
file: './tests/config-tests.ts',
timeout: 48000,
// To test only, say, 'push.spec.ts', do it as follows so that
// requests are authenticated:
Expand Down
14 changes: 8 additions & 6 deletions automation/build-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import { run as oclifRun } from '@oclif/core';
import { exec, execFile } from 'child_process';
import type { Stats } from 'fs';
import * as fs from 'fs-extra';
import * as klaw from 'klaw';
import * as path from 'path';
import * as rimraf from 'rimraf';
import fs from 'fs-extra';
import klaw from 'klaw';
import path from 'path';
import rimraf from 'rimraf';
import { promisify } from 'util';
import { notarize } from '@electron/notarize';

import { loadPackageJson, ROOT, whichSpawn } from './utils';
import { loadPackageJson, ROOT, whichSpawn } from './utils.js';

const execFileAsync = promisify(execFile);
const execAsync = promisify(exec);
Expand Down Expand Up @@ -347,6 +347,8 @@ export async function testShrinkwrap(): Promise<void> {
console.error(`[debug] platform=${process.platform}`);
}
if (process.platform !== 'win32') {
await whichSpawn(path.resolve(__dirname, 'test-lock-deduplicated.sh'));
await whichSpawn(
path.resolve(import.meta.dirname, 'test-lock-deduplicated.sh'),
);
}
}
10 changes: 5 additions & 5 deletions automation/capitanodoc/capitanodoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*/

import * as path from 'path';
import { MarkdownFileParser } from './utils';
import { GlobSync } from 'glob';
import path from 'path';
import { MarkdownFileParser } from './utils.js';
import glob from 'glob';

/**
* This is the skeleton of CLI documentation/reference web page at:
Expand Down Expand Up @@ -73,7 +73,7 @@ const commandHeadings: { [key: string]: string } = {
};

// Fetch all available commands
const allCommandsPaths = new GlobSync('build/commands/**/*.js', {
const allCommandsPaths = new glob.GlobSync('build/commands/**/*.js', {
ignore: 'build/commands/internal/**',
}).found;

Expand Down Expand Up @@ -135,7 +135,7 @@ capitanoDoc.categories.forEach((category) => {
* for the documentation web page.
*/
export async function getCapitanoDoc(): Promise<typeof capitanoDoc> {
const readmePath = path.join(__dirname, '..', '..', 'README.md');
const readmePath = path.join(import.meta.dirname, '..', '..', 'README.md');
const mdParser = new MarkdownFileParser(readmePath);
const sections: string[] = await Promise.all([
mdParser.getSectionOfTitle('About').then((sectionLines: string) => {
Expand Down
8 changes: 4 additions & 4 deletions automation/capitanodoc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as path from 'path';
import { getCapitanoDoc } from './capitanodoc';
import type { Category, Document, OclifCommand } from './doc-types';
import * as markdown from './markdown';
import path from 'path';
import { getCapitanoDoc } from './capitanodoc.js';
import type { Category, Document, OclifCommand } from './doc-types.js';
import * as markdown from './markdown.js';

/**
* Generates the markdown document (as a string) for the CLI documentation
Expand Down
6 changes: 3 additions & 3 deletions automation/capitanodoc/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/
import { Parser } from '@oclif/core';
import * as ent from 'ent';
import * as _ from 'lodash';
import _ from 'lodash';

import { capitanoizeOclifUsage } from '../../src/utils/oclif-utils';
import type { Category, Document } from './doc-types';
import { capitanoizeOclifUsage } from '../../src/utils/oclif-utils.js';
import type { Category, Document } from './doc-types.js';

function renderOclifCommand(command: Category['commands'][0]): string[] {
const result = [`## ${ent.encode(command.name || '')}`];
Expand Down
4 changes: 2 additions & 2 deletions automation/capitanodoc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* limitations under the License.
*/

import * as fs from 'fs';
import * as readline from 'readline';
import fs from 'fs';
import readline from 'readline';

export class MarkdownFileParser {
constructor(public mdFilePath: string) {}
Expand Down
6 changes: 3 additions & 3 deletions automation/check-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

// eslint-disable-next-line no-restricted-imports
import { stripIndent } from 'common-tags';
import * as _ from 'lodash';
import _ from 'lodash';
import { promises as fs } from 'fs';
import * as path from 'path';
import path from 'path';
import { simpleGit } from 'simple-git';

const ROOT = path.normalize(path.join(__dirname, '..'));
const ROOT = path.normalize(path.join(import.meta.dirname, '..'));

/**
* Compare the timestamp of balena-cli.md with the timestamp of staged files,
Expand Down
4 changes: 2 additions & 2 deletions automation/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
* limitations under the License.
*/

import * as _ from 'lodash';
import _ from 'lodash';

import {
buildOclifInstaller,
buildStandalone,
catchUncommitted,
signFilesForNotarization,
testShrinkwrap,
} from './build-bin';
} from './build-bin.js';

// DEBUG set to falsy for negative values else is truthy
process.env.DEBUG = ['0', 'no', 'false', '', undefined].includes(
Expand Down
5 changes: 4 additions & 1 deletion automation/update-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ interface Upstream {

const getUpstreams = async () => {
const fs = await import('fs');
const repoYaml = fs.readFileSync(__dirname + '/../repo.yml', 'utf8');
const repoYaml = fs.readFileSync(
import.meta.dirname + '/../repo.yml',
'utf8',
);

const yaml = await import('js-yaml');
const { upstream } = yaml.load(repoYaml) as {
Expand Down
8 changes: 4 additions & 4 deletions automation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/

import { spawn } from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import * as whichMod from 'which';
import path from 'path';
import fs from 'fs';
import whichMod from 'which';

export const ROOT = path.join(__dirname, '..');
export const ROOT = path.join(import.meta.dirname, '..');

export function loadPackageJson() {
const packageJsonPath = path.join(ROOT, 'package.json');
Expand Down
4 changes: 2 additions & 2 deletions bin/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ process.env.UV_THREADPOOL_SIZE = '64';
// `CONTRIBUTING.md`.

const path = require('path');
const rootDir = path.join(__dirname, '..');
const rootDir = path.join(import.meta.dirname, '..');

// Allow balena-dev to work with oclif by temporarily
// pointing oclif config options to src/ instead of build/
Expand Down Expand Up @@ -58,7 +58,7 @@ require('ts-node').register({
transpileOnly: true,
});
void require('../src/app').run(undefined, {
dir: __dirname,
dir: import.meta.dirname,
development: true,
});

Expand Down
12 changes: 7 additions & 5 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ process.env.OCLIF_TS_NODE = '0';

async function run() {
// Use fast-boot to cache require lookups, speeding up startup
await require('../build/fast-boot').start();
const { start } = await import('../build/fast-boot.js');
await start();

// Set the desired es version for downstream modules that support it
require('@balena/es-version').set('es2018');

// Run the CLI
await require('../build/app').run(undefined, { dir: __dirname });
(await import('@balena/es-version')).set('es2018');
//
// // Run the CLI
const { run } = await import('../build/app.js');
run(undefined, { dir: import.meta.url });
}

void run();
41 changes: 28 additions & 13 deletions completion/generate-completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Module from 'node:module';

const require = Module.createRequire(import.meta.url);
const path = require('path');
const rootDir = path.join(__dirname, '..');
const rootDir = path.join(import.meta.dirname, '..');
const fs = require('fs');
const { exit } = require('process');
const manifestFile = 'oclif.manifest.json';

commandsFilePath = path.join(rootDir, manifestFile);
const commandsFilePath = path.join(rootDir, manifestFile);
if (fs.existsSync(commandsFilePath)) {
console.log('Generating shell auto completion files...');
} else {
console.error(`generate-completion.js: Could not find "${manifestFile}"`);
process.exitCode = 1;
return;
exit(1);
}

const commandsJson = JSON.parse(fs.readFileSync(commandsFilePath, 'utf8'));
Expand All @@ -47,20 +50,28 @@ for (const key of Object.keys(commandsJson.commands).sort()) {
const mainCommandsStr = mainCommands.join(' ');

// GENERATE BASH COMPLETION FILE
bashFilePathIn = path.join(__dirname, '/templates/bash.template');
bashFilePathOut = path.join(__dirname, 'balena-completion.bash');
const bashFilePathIn = path.join(
import.meta.dirname,
'/templates/bash.template',
);
const bashFilePathOut = path.join(
import.meta.dirname,
'balena-completion.bash',
);

try {
fs.unlinkSync(bashFilePathOut);
} catch (error) {
process.exitCode = 1;
return console.error(error);
console.error(error);
exit(1);
}

fs.readFile(bashFilePathIn, 'utf8', function (err, data) {
if (err) {
process.exitCode = 1;
return console.error(err);
console.error(err);
exit(1);
}

data = data.replace(
Expand Down Expand Up @@ -103,26 +114,29 @@ fs.readFile(bashFilePathIn, 'utf8', function (err, data) {
fs.writeFile(bashFilePathOut, data, 'utf8', function (error) {
if (error) {
process.exitCode = 1;
return console.error(error);
console.error(error);
exit(1);
}
});
});

// GENERATE ZSH COMPLETION FILE
zshFilePathIn = path.join(__dirname, '/templates/zsh.template');
zshFilePathOut = path.join(__dirname, '_balena');
const zshFilePathIn = path.join(import.meta.dirname, '/templates/zsh.template');
const zshFilePathOut = path.join(import.meta.dirname, '_balena');

try {
fs.unlinkSync(zshFilePathOut);
} catch (error) {
process.exitCode = 1;
return console.error(error);
console.error(error);
exit(1);
}

fs.readFile(zshFilePathIn, 'utf8', function (err, data) {
if (err) {
process.exitCode = 1;
return console.error(err);
console.error(err);
exit(1);
}

data = data.replace(
Expand Down Expand Up @@ -169,7 +183,8 @@ fs.readFile(zshFilePathIn, 'utf8', function (err, data) {
fs.writeFile(zshFilePathOut, data, 'utf8', function (error) {
if (error) {
process.exitCode = 1;
return console.error(error);
console.error(error);
exit(1);
}
});
});
9 changes: 6 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const { FlatCompat } = require('@eslint/eslintrc');
import { FlatCompat } from '@eslint/eslintrc';
import Module from 'node:module';

const require = Module.createRequire(import.meta.url);

const compat = new FlatCompat({
baseDirectory: __dirname,
baseDirectory: import.meta.dirname,
});
module.exports = [
export default [
...require('@balena/lint/config/eslint.config'),
...compat.config({
parserOptions: {
Expand Down
Loading

0 comments on commit 31fc12a

Please sign in to comment.