Skip to content

Commit

Permalink
refactor: use node internal modules & replace fs-extra with node:fs (#…
Browse files Browse the repository at this point in the history
…273)

* refactor: use node:fs module instead of fs-extra
* refactor: use node:path module instead of path
* dep(npm): remove fs-extra
* refactor: use node:fs module instead of fs in electron app
* refactor: use node:os module instead of os
  • Loading branch information
erisu committed Apr 17, 2024
1 parent 545a25c commit da60500
Show file tree
Hide file tree
Showing 26 changed files with 213 additions and 167 deletions.
4 changes: 2 additions & 2 deletions bin/templates/platform_www/cdv-electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

const fs = require('fs');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const { cordova } = require('./package.json');
// Module to control application life, browser window and tray.
const {
Expand Down
10 changes: 5 additions & 5 deletions lib/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
this file is found by cordova-lib when you attempt to
'cordova platform add PATH' where path is this repo.
*/
const path = require('path');
const fs = require('fs-extra');
const path = require('node:path');
const fs = require('node:fs');
const {
ActionStack,
ConfigChanges: { PlatformMunger },
Expand Down Expand Up @@ -190,7 +190,7 @@ class Api {
this._removeModulesInfo(pluginInfo, targetDir);
// Remove stale plugin directory
// @todo this should be done by plugin files uninstaller
fs.removeSync(path.resolve(this.root, 'Plugins', pluginInfo.id));
fs.rmSync(path.resolve(this.root, 'Plugins', pluginInfo.id), { recursive: true, force: true });
});
}

Expand Down Expand Up @@ -303,8 +303,8 @@ class Api {
// BOTTOM OF METADATA
});`;

fs.ensureDirSync(targetDir);
fs.writeFileSync(path.join(targetDir, 'cordova_plugins.js'), final_contents, 'utf-8');
fs.mkdirSync(targetDir, { recursive: true });
fs.writeFileSync(path.join(targetDir, 'cordova_plugins.js'), final_contents, 'utf8');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/ManifestJsonParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

const fs = require('fs-extra');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');

class ManifestJsonParser {
constructor (wwwDir) {
Expand Down Expand Up @@ -91,7 +91,7 @@ class ManifestJsonParser {

if (fs.existsSync(startUrlPath)) {
// fetch start url file content and parse for theme-color.
const contents = fs.readFileSync(startUrlPath, 'utf-8');
const contents = fs.readFileSync(startUrlPath, 'utf8');
const result = /<meta(?=[^>]*name="theme-color")\s[^>]*content="([^>]*)"/i.exec(contents);

// If theme-color exists, the value is in index 1.
Expand Down
17 changes: 13 additions & 4 deletions lib/PackageJsonParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@
under the License.
*/

const fs = require('fs-extra');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const { getPackageJson } = require('./util');

class PackageJsonParser {
constructor (wwwDir, projectRootDir) {
// Electron App Package
// make sure www dir exists
if (!fs.existsSync(wwwDir)) {
fs.mkdirSync(wwwDir, { recursive: true });
}

this.path = path.join(wwwDir, 'package.json');
fs.ensureFileSync(this.path);

if (!fs.existsSync(this.path)) {
fs.writeFileSync(this.path, '{}', 'utf8');
}

// Electron App Package
this.package = JSON.parse(fs.readFileSync(this.path, 'utf8') || '{}');

// Force settings that are not allowed to change.
Expand Down
4 changes: 2 additions & 2 deletions lib/SettingJsonParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

const fs = require('fs-extra');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const { deepMerge } = require('./util');

class SettingJsonParser {
Expand Down
4 changes: 2 additions & 2 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

const fs = require('fs-extra');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const events = require('cordova-common').events;
const { deepMerge, getInstalledElectronVersion } = require('./util');

Expand Down
6 changes: 3 additions & 3 deletions lib/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

const fs = require('fs-extra');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const check_reqs = require('./check_reqs');
const platformBuildDir = path.join('platforms', 'electron', 'www');

Expand All @@ -32,7 +32,7 @@ module.exports.run = () => {

try {
if (fs.existsSync(platformBuildDir)) {
fs.removeSync(platformBuildDir);
fs.rmSync(platformBuildDir, { recursive: true, force: true });
}
} catch (err) {
console.log(`could not remove ${platformBuildDir} : ${err.message}`);
Expand Down
8 changes: 4 additions & 4 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

const fs = require('fs-extra');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const rootDir = path.resolve(__dirname, '..');
const events = require('cordova-common').events;
const check_reqs = require(path.join(rootDir, 'lib/check_reqs'));
Expand Down Expand Up @@ -46,10 +46,10 @@ module.exports.createProject = (platform_dir, package_name, project_name, option
}

// Make sure that the platform directory is created if missing.
fs.ensureDirSync(platform_dir);
fs.mkdirSync(platform_dir, { recursive: true });

// copy templates directory to the platform directory recursively
fs.copySync(path.join(rootDir, 'bin/templates'), path.join(platform_dir), { overwrite: false });
fs.cpSync(path.join(rootDir, 'bin/templates'), path.join(platform_dir), { recursive: true, force: true });

return Promise.resolve();
};
20 changes: 10 additions & 10 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

const path = require('path');
const fs = require('fs-extra');
const path = require('node:path');
const fs = require('node:fs');
const execa = require('execa');
const { events } = require('cordova-common');
const { _orderObject } = require('./PackageJsonParser');
Expand Down Expand Up @@ -53,7 +53,7 @@ module.exports = {
const moduleName = `${plugin_id}.${jsModule.name || path.basename(jsModule.src, path.extname(jsModule.src))}`;

// Read in the file, prepend the cordova.define, and write it back out.
let scriptContent = fs.readFileSync(moduleSource, 'utf-8').replace(/^\ufeff/, ''); // Window BOM
let scriptContent = fs.readFileSync(moduleSource, 'utf8').replace(/^\ufeff/, ''); // Window BOM

if (moduleSource.match(/.*\.json$/)) {
scriptContent = `module.exports = + ${scriptContent}`;
Expand All @@ -65,11 +65,11 @@ module.exports = {

const moduleDestination = path.resolve(www_dir, 'plugins', plugin_id, jsModule.src);

fs.ensureDirSync(path.dirname(moduleDestination));
fs.writeFileSync(moduleDestination, scriptContent, 'utf-8');
fs.mkdirSync(path.dirname(moduleDestination), { recursive: true });
fs.writeFileSync(moduleDestination, scriptContent, 'utf8');
},
uninstall: (jsModule, www_dir, plugin_id) => {
fs.removeSync(path.join(www_dir, 'plugins', plugin_id));
fs.rmSync(path.join(www_dir, 'plugins', plugin_id), { recursive: true, force: true });
const pluginRelativePath = path.join('plugins', plugin_id, jsModule.src);
// common.removeFileAndParents(www_dir, pluginRelativePath);
events.emit('verbose', `js-module uninstall called : ${pluginRelativePath}`);
Expand Down Expand Up @@ -226,12 +226,12 @@ module.exports = {
const dest = path.join(wwwDest, asset.target);
const destDir = path.parse(dest).dir;

fs.ensureDirSync(destDir);
fs.copySync(src, dest);
fs.mkdirSync(destDir, { recursive: true });
fs.cpSync(src, dest, { recursive: true });
},
uninstall: (asset, wwwDest, plugin_id) => {
fs.removeSync(path.join(wwwDest, asset.target));
fs.removeSync(path.join(wwwDest, 'plugins', plugin_id));
fs.rmSync(path.join(wwwDest, asset.target), { recursive: true, force: true });
fs.rmSync(path.join(wwwDest, 'plugins', plugin_id), { recursive: true, force: true });
}
}
};
6 changes: 3 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

const fs = require('fs-extra');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const CordovaError = require('cordova-common').CordovaError;
const events = require('cordova-common').events;
const FileUpdater = require('cordova-common').FileUpdater;
Expand Down Expand Up @@ -79,7 +79,7 @@ class Parser {
return this.update_from_config()
.then(
// Copy munged config.xml to platform www dir
() => fs.copySync(path.join(this.www_dir(), '..', 'config.xml'), path.join(this.www_dir(), 'config.xml'))
() => fs.cpSync(path.join(this.www_dir(), '..', 'config.xml'), path.join(this.www_dir(), 'config.xml'), { recursive: true })
);
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
under the License.
*/

const fs = require('fs-extra');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const { ConfigParser, xmlHelpers, events, CordovaError } = require('cordova-common');
const ManifestJsonParser = require('./ManifestJsonParser');
const PackageJsonParser = require('./PackageJsonParser');
Expand All @@ -35,13 +35,13 @@ module.exports.prepare = function (cordovaProject, options) {
// restored or copy project config into platform if none exists.
if (fs.existsSync(defaultConfigPath)) {
this.events.emit('verbose', `Generating config.xml from defaults for platform "${this.platform}"`);
fs.copySync(defaultConfigPath, ownConfigPath);
fs.cpSync(defaultConfigPath, ownConfigPath, { recursive: true });
} else if (fs.existsSync(ownConfigPath)) {
this.events.emit('verbose', `Generating defaults.xml from own config.xml for platform "${this.platform}"`);
fs.copySync(ownConfigPath, defaultConfigPath);
fs.cpSync(ownConfigPath, defaultConfigPath, { recursive: true });
} else {
this.events.emit('verbose', `case 3 "${this.platform}"`);
fs.copySync(sourceCfg.path, ownConfigPath);
fs.cpSync(sourceCfg.path, ownConfigPath, { recursive: true });
}

// merge our configs
Expand All @@ -63,7 +63,7 @@ module.exports.prepare = function (cordovaProject, options) {
// todo: validate it? ensure all properties we expect exist?
const manifestPath = path.join(this.locations.www, 'manifest.json');
this.events.emit('verbose', `Copying ${srcManifestPath} => ${manifestPath}`);
fs.copySync(srcManifestPath, manifestPath);
fs.cpSync(srcManifestPath, manifestPath, { recursive: true });
} else {
this.events.emit('verbose', `Creating new manifest file in => ${this.path}`);

Expand Down Expand Up @@ -345,7 +345,7 @@ function copyResources (rootDir, resourceMap) {

if (elementKeys.length) {
const value = elementKeys.map((e) => element[e])[0];
fs.copySync(path.join(rootDir, elementKeys[0]), value);
fs.cpSync(path.join(rootDir, elementKeys[0]), value, { recursive: true });
}
});
}
2 changes: 1 addition & 1 deletion lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const electron = require('electron');
const execa = require('execa');
const path = require('path');
const path = require('node:path');

module.exports.run = function (args = {}) {
const electonArgs = args.argv || [];
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"electron": "^29.0.0",
"electron-builder": "^24.12.0",
"electron-devtools-installer": "^3.2.0",
"execa": "^5.1.1",
"fs-extra": "^11.2.0"
"execa": "^5.1.1"
},
"devDependencies": {
"@cordova/eslint-config": "^5.0.0",
Expand Down
Loading

0 comments on commit da60500

Please sign in to comment.