Skip to content

Commit

Permalink
Convert to ESM
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
Page- committed Dec 30, 2024
1 parent 8007f54 commit 617b662
Show file tree
Hide file tree
Showing 116 changed files with 485 additions and 485 deletions.
31 changes: 9 additions & 22 deletions Gruntfile.ts → Gruntfile.cts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { WebpackPluginInstance } from 'webpack';

import _ from 'lodash';
import TerserPlugin from 'terser-webpack-plugin';
import browserConfig from './build/browser';
import moduleConfig from './build/module';
import serverConfig from './build/server';
import browserConfig from './build/browser.cts';
import moduleConfig from './build/module.cts';
import serverConfig from './build/server.cts';

const serverConfigs = {
browser: browserConfig,
Expand Down Expand Up @@ -110,30 +110,18 @@ export = (grunt: typeof Grunt) => {
return renames;
})(),

replace: {
'pine.js': {
replace: _.mapValues(serverConfigs, (_config, task) => {
return {
src: 'out/pine.js',
overwrite: true,
replacements: [
{
from: /nodeRequire/g,
to: 'require',
from: /sourceMappingURL=pine.js.map/g,
to: `sourceMappingURL=pine-${task}-<%= grunt.option('version') %>.js.map`,
},
],
},
..._.mapValues(serverConfigs, (_config, task) => {
return {
src: 'out/pine.js',
overwrite: true,
replacements: [
{
from: /sourceMappingURL=pine.js.map/g,
to: `sourceMappingURL=pine-${task}-<%= grunt.option('version') %>.js.map`,
},
],
};
}),
},
};
}),

webpack: serverConfigs,

Expand Down Expand Up @@ -167,7 +155,6 @@ export = (grunt: typeof Grunt) => {
'webpack:' + task,
'gitinfo:describe',
'version',
'replace:pine.js',
`replace:${task}`,
`concat:${task}`,
`rename:${task}`,
Expand Down
2 changes: 1 addition & 1 deletion bin/abstract-sql-compiler.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../out/bin/abstract-sql-compiler');
import '../out/bin/abstract-sql-compiler.js';
2 changes: 1 addition & 1 deletion bin/odata-compiler.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../out/bin/odata-compiler');
import '../out/bin/odata-compiler.js';
2 changes: 1 addition & 1 deletion bin/sbvr-compiler.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('../out/bin/sbvr-compiler');
import '../out/bin/sbvr-compiler.js';
2 changes: 1 addition & 1 deletion build/browser.ts → build/browser.cts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as webpack from 'webpack';
import type { Configuration } from 'webpack';
import sharedConfig from './config';
import sharedConfig from './config.cts';

if (typeof sharedConfig.externals !== 'object') {
throw new Error('Expected externals to be an object');
Expand Down
6 changes: 5 additions & 1 deletion build/config.ts → build/config.cts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RequiredField } from '../src/sbvr-api/common-types';
import type { RequiredField } from '../src/sbvr-api/common-types.js' with { 'resolution-mode': 'import' };

import * as path from 'path';
import * as webpack from 'webpack';
Expand Down Expand Up @@ -46,6 +46,10 @@ const config: RequiredField<
},
resolve: {
extensions: ['.js', '.ts'],
extensionAlias: {
'.js': ['.ts', '.js'],
'.mjs': ['.mts', '.mjs'],
},
},
plugins: [new webpack.optimize.LimitChunkCountPlugin({ maxChunks: 1 })],
module: {
Expand Down
2 changes: 1 addition & 1 deletion build/module.ts → build/module.cts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as webpack from 'webpack';
import type { Configuration } from 'webpack';
import sharedConfig from './config';
import sharedConfig from './config.cts';

const config: Configuration = {
...sharedConfig,
Expand Down
2 changes: 1 addition & 1 deletion build/server.ts → build/server.cts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as webpack from 'webpack';
import type { Configuration } from 'webpack';
import sharedConfig from './config';
import sharedConfig from './config.cts';

const config: Configuration = {
...sharedConfig,
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@balena/pinejs",
"version": "19.7.1",
"main": "out/server-glue/module",
"type": "commonjs",
"type": "module",
"repository": "[email protected]:balena-io/pinejs.git",
"license": "Apache-2.0",
"bin": {
Expand All @@ -13,17 +13,17 @@
"scripts": {
"prepublish": "require-npm4-to-publish",
"prepare": "node -e \"try { (await import('husky')).default() } catch (e) { if (e.code !== 'ERR_MODULE_NOT_FOUND') throw e }\" --input-type module && npm run build",
"build": "grunt build",
"webpack-browser": "grunt browser",
"webpack-module": "grunt module",
"webpack-server": "grunt server",
"build": "grunt --preload ts-node/register/transpile-only --gruntfile Gruntfile.cts build",
"webpack-browser": "grunt --preload ts-node/register/transpile-only --gruntfile Gruntfile.cts browser",
"webpack-module": "grunt --preload ts-node/register/transpile-only --gruntfile Gruntfile.cts module",
"webpack-server": "grunt --preload ts-node/register/transpile-only --gruntfile Gruntfile.cts server",
"webpack-build": "npm run webpack-browser && npm run webpack-module && npm run webpack-server",
"lint": "balena-lint -t tsconfig.dev.json -e js -e ts src build typings Gruntfile.ts && npx tsc --project tsconfig.dev.json --noEmit",
"lint": "balena-lint -t tsconfig.dev.json -e js -e ts src build typings Gruntfile.cts && npx tsc --project tsconfig.dev.json --noEmit",
"test": "npm run lint && npm run build && npm run webpack-build && npm run test:compose && npm run test:generated-types",
"test:compose": "trap 'docker compose -f docker-compose.npm-test.yml down ; echo Stopped ; exit 0' INT; docker compose -f docker-compose.npm-test.yml up -d && sleep 2 && DATABASE_URL=postgres://docker:docker@localhost:5431/postgres PINEJS_WEBRESOURCE_MAXFILESIZE=1000000000 S3_ENDPOINT=http://localhost:43680 S3_ACCESS_KEY=USERNAME S3_SECRET_KEY=PASSWORD S3_STORAGE_ADAPTER_BUCKET=balena-pine-web-resources S3_REGION=us-east-1 PINEJS_QUEUE_CONCURRENCY=1 TZ=UTC npm run mocha",
"test:generated-types": "npm run generate-types && git diff --exit-code ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts",
"mocha": "TS_NODE_FILES=true mocha",
"lint-fix": "balena-lint -t tsconfig.dev.json -e js -e ts --fix src test build typings Gruntfile.ts",
"lint-fix": "balena-lint -t tsconfig.dev.json -e js -e ts --fix src test build typings Gruntfile.cts",
"generate-types": "node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/user.sbvr ./src/sbvr-api/user.ts && node ./bin/sbvr-compiler.js generate-types ./src/migrator/migrations.sbvr ./src/migrator/migrations.ts && node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/dev.sbvr ./src/sbvr-api/dev.ts && node ./bin/sbvr-compiler.js generate-types ./src/tasks/tasks.sbvr ./src/tasks/tasks.ts && balena-lint -t tsconfig.dev.json --fix ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts"
},
"dependencies": {
Expand Down Expand Up @@ -141,7 +141,7 @@
"extension": [
".test.ts"
],
"require": "ts-node/register/transpile-only",
"loader": "ts-node/esm/transpile-only",
"exit": true,
"timeout": 60000,
"recursive": true
Expand Down
6 changes: 3 additions & 3 deletions src/bin/abstract-sql-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {
version,
writeAll,
writeSqlModel,
} from './utils';
} from './utils.js';

import { program } from 'commander';

const runCompile = async (inputFile: string, outputFile?: string) => {
const { generateSqlModel } = await import('../sbvr-api/sbvr-utils.js');
const abstractSql = getAbstractSqlModelFromFile(
const abstractSql = await getAbstractSqlModelFromFile(
inputFile,
program.opts().model,
);
Expand All @@ -28,7 +28,7 @@ const generateTypes = async (
const { abstractSqlToTypescriptTypes } = await import(
'@balena/abstract-sql-to-typescript/generate'
);
const abstractSql = getAbstractSqlModelFromFile(
const abstractSql = await getAbstractSqlModelFromFile(
inputFile,
program.opts().model,
);
Expand Down
6 changes: 3 additions & 3 deletions src/bin/odata-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAbstractSqlModelFromFile, version, writeAll } from './utils';
import { getAbstractSqlModelFromFile, version, writeAll } from './utils.js';
import type {
AbstractSqlModel,
SqlResult,
Expand Down Expand Up @@ -44,7 +44,7 @@ const translateOData = async (
outputFile?: string,
) => {
const request = await generateAbstractSqlQuery(
getAbstractSqlModelFromFile(modelFile, program.opts().model),
await getAbstractSqlModelFromFile(modelFile, program.opts().model),
odata,
);
const json = JSON.stringify(request.abstractSqlQuery, null, 2);
Expand All @@ -68,7 +68,7 @@ const compileOData = async (
outputFile?: string,
) => {
const translatedRequest = await generateAbstractSqlQuery(
getAbstractSqlModelFromFile(modelFile, program.opts().model),
await getAbstractSqlModelFromFile(modelFile, program.opts().model),
odata,
);
const { compileRequest } = await import('../sbvr-api/abstract-sql.js');
Expand Down
2 changes: 1 addition & 1 deletion src/bin/sbvr-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { version, writeAll, writeSqlModel } from './utils';
import { version, writeAll, writeSqlModel } from './utils.js';
import { program } from 'commander';
import * as fs from 'fs';

Expand Down
31 changes: 18 additions & 13 deletions src/bin/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
process.env.PINEJS_CACHE_FILE =
process.env.PINEJS_CACHE_FILE || __dirname + '/.pinejs-cache.json';
process.env.PINEJS_CACHE_FILE ||

Check warning on line 2 in src/bin/utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (20.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator

Check warning on line 2 in src/bin/utils.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test npm (22.x)

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
fileURLToPath(new URL('.pinejs-cache.json', import.meta.url));

import type { SqlModel } from '@balena/abstract-sql-compiler';
import type { Config, Model } from '../config-loader/config-loader';
import type * as SbvrUtils from '../sbvr-api/sbvr-utils';
import type { Config, Model } from '../config-loader/config-loader.js';
import type { AbstractSqlModel } from '@balena/abstract-sql-compiler';

import * as fs from 'fs';
import * as path from 'path';
import '../server-glue/sbvr-loader';
import '../server-glue/sbvr-loader.js';
import { fileURLToPath } from 'url';
import { loadSBVR } from '../server-glue/sbvr-loader.js';

export { version } from '../config-loader/env';
export { version } from '../config-loader/env.js';

export const writeAll = (output: string, outputFile?: string): void => {
if (outputFile) {
Expand Down Expand Up @@ -68,15 +70,18 @@ const getConfigModel = (
return fileContents;
};

export const getAbstractSqlModelFromFile = (
export const getAbstractSqlModelFromFile = async (
modelFile: string,
modelName: string | undefined,
): AbstractSqlModel => {
): Promise<AbstractSqlModel> => {
let fileContents: string | Model | AbstractSqlModel | Config;
try {
fileContents = require(path.resolve(modelFile));
fileContents = await import(path.resolve(modelFile));
} catch {
fileContents = fs.readFileSync(require.resolve(modelFile), 'utf8');
fileContents = fs.readFileSync(
new URL(import.meta.resolve(modelFile)),
'utf8',
);
}
let seModel: string;
if (fileContents == null) {
Expand All @@ -94,16 +99,16 @@ export const getAbstractSqlModelFromFile = (
} else if ('modelText' in configModel && configModel.modelText != null) {
seModel = configModel.modelText;
} else if ('modelFile' in configModel && configModel.modelFile != null) {
seModel = fs.readFileSync(require.resolve(configModel.modelFile), 'utf8');
seModel = await loadSBVR(configModel.modelFile, import.meta);
} else {
throw new Error('Unrecognized config file');
}
} else {
throw new Error('Unrecognized config file');
}
const { generateLfModel, generateAbstractSqlModel } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('../sbvr-api/sbvr-utils') as typeof SbvrUtils;
const { generateLfModel, generateAbstractSqlModel } = await import(
'../sbvr-api/sbvr-utils.js'
);
let lfModel;
try {
lfModel = generateLfModel(seModel);
Expand Down
18 changes: 9 additions & 9 deletions src/config-loader/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import type {
AbstractSqlModel,
Definition,
} from '@balena/abstract-sql-compiler';
import type { Database } from '../database-layer/db';
import type { Database } from '../database-layer/db.js';
import type {
AnyObject,
Dictionary,
RequiredField,
Resolvable,
} from '../sbvr-api/common-types';
} from '../sbvr-api/common-types.js';

import {
type Migration,
Expand All @@ -18,23 +18,23 @@ import {
MigrationCategories,
isSyncMigration,
isAsyncMigration,
} from '../migrator/utils';
} from '../migrator/utils.js';

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

import * as sbvrUtils from '../sbvr-api/sbvr-utils';
import * as sbvrUtils from '../sbvr-api/sbvr-utils.js';

import * as permissions from '../sbvr-api/permissions';
import * as permissions from '../sbvr-api/permissions.js';
import {
getDefaultHandler,
getUploaderMiddlware,
type WebResourceHandler,
setupUploadHooks,
setupWebresourceHandler,
} from '../webresource-handler';
import type { AliasValidNodeType } from '../sbvr-api/translations';
} from '../webresource-handler/index.js';
import type { AliasValidNodeType } from '../sbvr-api/translations.js';

export type SetupFunction = (
app: Express.Application,
Expand Down Expand Up @@ -256,7 +256,7 @@ export const setup = (app: Express.Application) => {
let customCode: SetupFunction;
if (typeof model.customServerCode === 'string') {
try {
customCode = nodeRequire(model.customServerCode).setup;
customCode = (await import(model.customServerCode)).setup;
} catch (e: any) {
e.message = `Error loading custom server code: '${e.message}'`;
throw e;
Expand Down Expand Up @@ -291,7 +291,7 @@ export const setup = (app: Express.Application) => {
let root: string;
let configObj: Config;
if (config == null) {
root = path.resolve(process.argv[2]) || __dirname;
root = path.resolve(process.argv[2]) || import.meta.dirname;
configObj = await loadConfigFile(path.join(root, 'config.json'));
} else if (typeof config === 'string') {
root = path.dirname(config);
Expand Down
4 changes: 2 additions & 2 deletions src/config-loader/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
export const { version } = JSON.parse(
fs.readFileSync(require.resolve('../../package.json'), 'utf8'),
fs.readFileSync(new URL(import.meta.resolve('../../package.json')), 'utf8'),
);

const { PINEJS_DEBUG } = process.env;
Expand Down Expand Up @@ -56,7 +56,7 @@ export const cache = {

import { boolVar, intVar } from '@balena/env-parsing';
import memoize from 'memoizee';
import memoizeWeak from 'memoizee/weak';
import memoizeWeak from 'memoizee/weak.js';
export const createCache = <T extends (...args: any[]) => any>(
cacheName: keyof typeof cache,
fn: T,
Expand Down
8 changes: 4 additions & 4 deletions src/data-server/sbvr-server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as permissions from '../sbvr-api/permissions';
import type { Resolvable } from '../sbvr-api/common-types';
import * as permissions from '../sbvr-api/permissions.js';
import type { Resolvable } from '../sbvr-api/common-types.js';
import type { Handler } from 'express';
import type { Config, SetupFunction } from '../config-loader/config-loader';
import type { Tx } from '../database-layer/db';
import type { Config, SetupFunction } from '../config-loader/config-loader.js';
import type { Tx } from '../database-layer/db.js';

const uiModel = `\
Vocabulary: ui
Expand Down
Loading

0 comments on commit 617b662

Please sign in to comment.