Skip to content

Commit

Permalink
Migrate project to ESM
Browse files Browse the repository at this point in the history
Closes #135.

Co-Authored-By: Darryl Pogue <[email protected]>
  • Loading branch information
hereje and dpogue committed Sep 19, 2023
1 parent d2a5105 commit d6fe78e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 86 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"root": true,
"parserOptions": {
"ecmaVersion": "2015"
"ecmaVersion": "2022",
"sourceType": "module"
},
"rules": {
"brace-style": ["error", "1tbs"],
Expand Down
26 changes: 13 additions & 13 deletions bin/seymour → bin/seymour.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
* limitations under the License.
*/

require('loud-rejection/register');
import 'loud-rejection/register.js';
import util from 'node:util';
import cordovaCommon from 'cordova-common';
import seymour from 'seymour';

var events = require('cordova-common').events;
var util = require('util');
var seymour = require('../src/seymour');
seymour(process.argv, process.env)
.catch((err) => {
if (!(err instanceof Error)) {
throw new Error(`Promise rejected with value: ${util.inspect(err)}`);
}

seymour(process.argv, process.env).catch(function(err) {
if (!(err instanceof Error)) {
throw new Error('Promise rejected with value: ' + util.inspect(err));
}
process.exitCode = err.code || 1;

process.exitCode = err.code || 1;

console.error(err);
events.emit('verbose', err.stack);
});
console.error(err);
cordovaCommon.events.emit('verbose', err.stack);
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "seymour",
"version": "6.0.3",
"type": "module",
"description": "A build helper for Apache Cordova projects.",
"repository": {
"type": "git",
Expand All @@ -13,11 +14,15 @@
],
"license": "Apache-2.0",
"main": "src/seymour.js",
"bin": "bin/seymour",
"bin": "bin/seymour.js",
"files": [
"bin",
"src"
],
"exports": {
".": "./src/seymour.js",
"./package.json": "./package.json"
},
"scripts": {
"lint": "eslint .",
"test": "node --test",
Expand Down
108 changes: 49 additions & 59 deletions src/seymour.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,41 @@
* limitations under the License.
*/

var pkg = require('../package.json');
var cordova = require('cordova-lib').cordova;
var ConfigParser = require('cordova-common').ConfigParser;
var CordovaError = require('cordova-common').CordovaError;
var events = require('cordova-common').events;
var CordovaLogger = require('cordova-common').CordovaLogger;
var path = require('path');
var HooksRunner = require('cordova-lib/src/hooks/HooksRunner');
var cordovaUtil = require('cordova-lib/src/cordova/util');

function run(args, env) {
import path from 'node:path';
import { createRequire } from 'node:module';
import cordovaLib from 'cordova-lib';
import cordovaCommon from 'cordova-common';
import cordovaUtil from 'cordova-lib/src/cordova/util.js';
import HooksRunner from 'cordova-lib/src/hooks/HooksRunner.js';

const { ConfigParser, CordovaError, events, CordovaLogger } = cordovaCommon;
const { cordova } = cordovaLib;

export default function run(args, env) {
if (args.indexOf('-v') !== -1 || args.indexOf('--version') !== -1) {
var cdvVer = require('cordova-lib/package').version;
const require = createRequire(import.meta.url);
const cdvPkg = require('cordova-lib/package.json');
const seyPkg = require('../package.json');

console.log('Seymour ' + pkg.version);
console.log('Cordova ' + cdvVer);
console.log(`Seymour ${seyPkg.version}`);
console.log(`Cordova ${cdvPkg.version}`);
return Promise.resolve();
}

var projectRoot = cordova.findProjectRoot(process.cwd());
const projectRoot = cordova.findProjectRoot(process.cwd());
if (!projectRoot) {
return Promise.reject(new CordovaError('Current working directory is not a Cordova-based project: ' + process.cwd()));
return Promise.reject(new CordovaError(`Current working directory is not a Cordova-based project: ${process.cwd()}`));
}

var logger = CordovaLogger.get();
const logger = CordovaLogger.get();
logger.subscribe(events);

var configPath = path.join(projectRoot, 'config.xml');
var config = new ConfigParser(configPath);
const configPath = path.join(projectRoot, 'config.xml');
const config = new ConfigParser(configPath);

var opts = {
const opts = {
platforms: [],
options: {device: true},
options: { device: true },
verbose: false,
silent: false,
fetch: true
Expand Down Expand Up @@ -78,9 +80,7 @@ function run(args, env) {
if (env.SEY_BUILD_PLATFORMS) {
opts.platforms = env.SEY_BUILD_PLATFORMS
.split(',')
.map(function(p) {
return p.toLowerCase();
});
.map((p) => p.toLowerCase());
}

if (env.SEY_BUILD_MODE && env.SEY_BUILD_MODE.toLowerCase() === 'release') {
Expand All @@ -94,7 +94,7 @@ function run(args, env) {
}

if (env.SEY_BUILD_NUMBER) {
var attrs = [
const attrs = [
'android-versionCode',
'ios-CFBundleVersion',
'osx-CFBundleVersion'
Expand All @@ -108,23 +108,19 @@ function run(args, env) {
}

Object.keys(env)
.filter(function(v) {
return v.match(/^SEY_PREFERENCE_/);
})
.filter((v) => v.match(/^SEY_PREFERENCE_/))
.forEach(function(envName) {
var name = envName.replace(/^SEY_PREFERENCE_/, '');
const name = envName.replace(/^SEY_PREFERENCE_/, '');
config.setGlobalPreference(name, env[envName]);
});

Object.keys(env)
.filter(function(v) {
return v.match(/^SEY_([A-Za-z]+)_PREFERENCE_/);
})
.filter((v) => v.match(/^SEY_([A-Za-z]+)_PREFERENCE_/))
.forEach(function(envName) {
var name = envName.replace(/^SEY_([A-Za-z]+)_PREFERENCE_/, '');
var platform = envName.match(/^SEY_([A-Za-z]+)(?=_)/)[0] //e.g. SEY_IOS
.replace(/^SEY_/, '') // Remove SEY_
.toLowerCase();
const name = envName.replace(/^SEY_([A-Za-z]+)_PREFERENCE_/, '');
const platform = envName.match(/^SEY_([A-Za-z]+)(?=_)/)[0] //e.g. SEY_IOS
.replace(/^SEY_/, '') // Remove SEY_
.toLowerCase();
config.setPlatformPreference(name, platform, env[envName]);
});

Expand All @@ -136,30 +132,24 @@ function run(args, env) {
}


var base_opts = JSON.stringify(opts);

var prep_opts = JSON.parse(base_opts);
const base_opts = JSON.stringify(opts);
const prep_opts = JSON.parse(base_opts);

return cordova.prepare.call(null, prep_opts)
.then(function() {
// Some plugins (Crosswalk *shakefist*) add a bunch of their own stuff
// to config.xml that overrides user-defined variables.
// We re-save the config.xml file after installing plugins to ensure
// we have the data that we want and not extra garbage.
config.write();

var projectRoot = cordovaUtil.cdProjectRoot();
var build_opts = cordovaUtil.preProcessOptions(JSON.parse(base_opts));

var hooksRunner = new HooksRunner(projectRoot);

return hooksRunner.fire('before_build', build_opts)
.then(function() {
return cordova.compile.call(null, build_opts);
})
.then(function() {
return hooksRunner.fire('after_build', build_opts);
.then(() => {
// Some plugins (Crosswalk *shakefist*) add a bunch of their own stuff
// to config.xml that overrides user-defined variables.
// We re-save the config.xml file after installing plugins to ensure
// we have the data that we want and not extra garbage.
config.write();

const projectRoot = cordovaUtil.cdProjectRoot();
const build_opts = cordovaUtil.preProcessOptions(JSON.parse(base_opts));

const hooksRunner = new HooksRunner(projectRoot);

return hooksRunner.fire('before_build', build_opts)
.then(() => cordova.compile.call(null, build_opts))
.then(() => hooksRunner.fire('after_build', build_opts));
});
});
}
module.exports = run;
32 changes: 20 additions & 12 deletions test/seymour.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,36 @@
* limitations under the License.
*/

var test = require('node:test');
var assert = require('node:assert');
var fs = require('node:fs');
var path = require('node:path');
var seymour = require('../src/seymour');
var cordova = require('cordova-lib').cordova;
var ConfigParser = require('cordova-common').ConfigParser;
var CordovaLogger = require('cordova-common').CordovaLogger;
var CordovaError = require('cordova-common').CordovaError;

import test from 'node:test';
import assert from 'node:assert';
import node_fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import url from 'node:url';
import cordovaLib from 'cordova-lib';
import cordovaCommon from 'cordova-common';
import seymour from 'seymour';
//import pkgJson from 'seymour/package.json' assert { type: 'json' };

let fs = node_fs;
try {
fs = require('fs-extra');
fs = (await import('fs-extra')).default;
} catch (e) { }

const { cordova } = cordovaLib;
const { ConfigParser, CordovaLogger, CordovaError } = cordovaCommon;

const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
process.env.PWD = __dirname;
process.chdir(__dirname);


test('version', function(t) {
t.mock.method(console, 'log', function() {});

var version = require('../package.json').version;
const require = createRequire(import.meta.url);
const pkgJson = require('seymour/package.json');
const version = pkgJson.version;

return Promise.all([
t.test('with --version', function() {
Expand Down

0 comments on commit d6fe78e

Please sign in to comment.