Skip to content

Commit

Permalink
refactor: replace fs-extra with node:fs (#1772)
Browse files Browse the repository at this point in the history
* spec: add devDependencies "tmp"
  • Loading branch information
erisu authored Jan 29, 2025
1 parent b623311 commit e012478
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 164 deletions.
14 changes: 7 additions & 7 deletions lib/builders/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
under the License.
*/

const fs = require('fs-extra');
const fs = require('node:fs');
const path = require('node:path');
const execa = require('execa');
const glob = require('fast-glob');
Expand Down Expand Up @@ -174,7 +174,7 @@ class ProjectBuilder {
try {
fs.accessSync(subProjectGradle, fs.F_OK);
} catch (e) {
fs.copySync(pluginBuildGradle, subProjectGradle);
fs.cpSync(pluginBuildGradle, subProjectGradle);
}
};

Expand Down Expand Up @@ -207,7 +207,7 @@ class ProjectBuilder {
settingsGradlePaths.join(''));

// Touch empty cdv-gradle-name.gradle file if missing.
if (!fs.pathExistsSync(path.join(this.root, 'cdv-gradle-name.gradle'))) {
if (!fs.existsSync(path.join(this.root, 'cdv-gradle-name.gradle'))) {
fs.writeFileSync(path.join(this.root, 'cdv-gradle-name.gradle'), '');
}

Expand Down Expand Up @@ -294,7 +294,7 @@ class ProjectBuilder {
}).then(() => {
const signingPropertiesPath = path.join(self.root, `${opts.buildType}${SIGNING_PROPERTIES}`);

if (fs.existsSync(signingPropertiesPath)) fs.removeSync(signingPropertiesPath);
if (fs.existsSync(signingPropertiesPath)) fs.rmSync(signingPropertiesPath);
if (opts.packageInfo) {
fs.ensureFileSync(signingPropertiesPath);
const signingProperties = createEditor(signingPropertiesPath);
Expand All @@ -309,7 +309,7 @@ class ProjectBuilder {
* @returns The user defined configs
*/
_getCordovaConfig () {
return fs.readJSONSync(path.join(this.root, 'cdv-gradle-config.json'));
return JSON.parse(fs.readFileSync(path.join(this.root, 'cdv-gradle-config.json'), 'utf-8') || '{}');
}

/*
Expand Down Expand Up @@ -342,15 +342,15 @@ class ProjectBuilder {
const args = this.getArgs('clean', opts);
return execa(wrapper, args, { stdio: 'inherit', cwd: path.resolve(this.root) })
.then(() => {
fs.removeSync(path.join(this.root, 'out'));
fs.rmSync(path.join(this.root, 'out'), { recursive: true, force: true });

['debug', 'release'].map(config => path.join(this.root, `${config}${SIGNING_PROPERTIES}`))
.forEach(file => {
const hasFile = fs.existsSync(file);
const hasMarker = hasFile && fs.readFileSync(file, 'utf8')
.includes(MARKER);

if (hasFile && hasMarker) fs.removeSync(file);
if (hasFile && hasMarker) fs.rmSync(file);
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/check_reqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const execa = require('execa');
const path = require('node:path');
const fs = require('fs-extra');
const fs = require('node:fs');
const { forgivingWhichSync, isWindows, isDarwin } = require('./utils');
const java = require('./env/java');
const { CordovaError, ConfigParser, events } = require('cordova-common');
Expand Down
6 changes: 3 additions & 3 deletions lib/config/CordovaGradleConfigParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
under the License.
*/

const fs = require('fs-extra');
const fs = require('node:fs');
const path = require('node:path');
const events = require('cordova-common').events;

Expand All @@ -41,7 +41,7 @@ class CordovaGradleConfigParser {
* @returns {Record<any, any>} The parsed JSON object representing the gradle config.
*/
_readConfig (configPath) {
return fs.readJSONSync(configPath, 'utf-8');
return JSON.parse(fs.readFileSync(configPath, 'utf-8') || '{}');
}

setPackageName (packageName) {
Expand All @@ -64,7 +64,7 @@ class CordovaGradleConfigParser {
*/
write () {
events.emit('verbose', '[Cordova Gradle Config] Saving File');
fs.writeJSONSync(this._cdvGradleConfigFilePath, this._cdvGradleConfig, 'utf-8');
fs.writeFileSync(this._cdvGradleConfigFilePath, JSON.stringify(this._cdvGradleConfig, null, 2), 'utf-8');
}
}

Expand Down
56 changes: 28 additions & 28 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

const path = require('node:path');
const fs = require('fs-extra');
const fs = require('node:fs');
const utils = require('./utils');
const check_reqs = require('./check_reqs');
const ROOT = path.join(__dirname, '..');
Expand Down Expand Up @@ -49,27 +49,27 @@ function copyJsAndLibrary (projectPath, shared, projectName, targetAPI) {
const app_path = path.join(projectPath, 'app', 'src', 'main');
const platform_www = path.join(projectPath, 'platform_www');

fs.copySync(srcCordovaJsPath, path.join(app_path, 'assets', 'www', 'cordova.js'));
fs.cpSync(srcCordovaJsPath, path.join(app_path, 'assets', 'www', 'cordova.js'));

// Copy the cordova.js file to platforms/<platform>/platform_www/
// The www dir is nuked on each prepare so we keep cordova.js in platform_www
fs.ensureDirSync(platform_www);
fs.copySync(srcCordovaJsPath, path.join(platform_www, 'cordova.js'));
fs.copySync(path.join(ROOT, 'framework', 'cdv-gradle-config-defaults.json'), path.join(projectPath, 'cdv-gradle-config.json'));
fs.mkdirSync(platform_www, { recursive: true });
fs.cpSync(srcCordovaJsPath, path.join(platform_www, 'cordova.js'));
fs.cpSync(path.join(ROOT, 'framework', 'cdv-gradle-config-defaults.json'), path.join(projectPath, 'cdv-gradle-config.json'));

if (shared) {
const relativeFrameworkPath = path.relative(projectPath, getFrameworkDir(projectPath, true));
fs.symlinkSync(relativeFrameworkPath, nestedCordovaLibPath, 'dir');
} else {
fs.ensureDirSync(nestedCordovaLibPath);
fs.copySync(path.join(ROOT, 'framework', 'AndroidManifest.xml'), path.join(nestedCordovaLibPath, 'AndroidManifest.xml'));
fs.mkdirSync(nestedCordovaLibPath, { recursive: true });
fs.cpSync(path.join(ROOT, 'framework', 'AndroidManifest.xml'), path.join(nestedCordovaLibPath, 'AndroidManifest.xml'));
const propertiesEditor = createEditor(path.join(ROOT, 'framework', 'project.properties'));
propertiesEditor.set('target', targetAPI);
propertiesEditor.save(path.join(nestedCordovaLibPath, 'project.properties'));
fs.copySync(path.join(ROOT, 'framework', 'build.gradle'), path.join(nestedCordovaLibPath, 'build.gradle'));
fs.copySync(path.join(ROOT, 'framework', 'cordova.gradle'), path.join(nestedCordovaLibPath, 'cordova.gradle'));
fs.copySync(path.join(ROOT, 'framework', 'repositories.gradle'), path.join(nestedCordovaLibPath, 'repositories.gradle'));
fs.copySync(path.join(ROOT, 'framework', 'src'), path.join(nestedCordovaLibPath, 'src'));
fs.cpSync(path.join(ROOT, 'framework', 'build.gradle'), path.join(nestedCordovaLibPath, 'build.gradle'));
fs.cpSync(path.join(ROOT, 'framework', 'cordova.gradle'), path.join(nestedCordovaLibPath, 'cordova.gradle'));
fs.cpSync(path.join(ROOT, 'framework', 'repositories.gradle'), path.join(nestedCordovaLibPath, 'repositories.gradle'));
fs.cpSync(path.join(ROOT, 'framework', 'src'), path.join(nestedCordovaLibPath, 'src'), { recursive: true });
}
}

Expand Down Expand Up @@ -116,10 +116,10 @@ function prepBuildFiles (projectPath) {
function copyBuildRules (projectPath) {
const srcDir = path.join(ROOT, 'templates', 'project');

fs.copySync(path.join(srcDir, 'build.gradle'), path.join(projectPath, 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'build.gradle'), path.join(projectPath, 'app', 'build.gradle'));
fs.copySync(path.join(srcDir, 'app', 'repositories.gradle'), path.join(projectPath, 'app', 'repositories.gradle'));
fs.copySync(path.join(srcDir, 'repositories.gradle'), path.join(projectPath, 'repositories.gradle'));
fs.cpSync(path.join(srcDir, 'build.gradle'), path.join(projectPath, 'build.gradle'));
fs.cpSync(path.join(srcDir, 'app', 'build.gradle'), path.join(projectPath, 'app', 'build.gradle'));
fs.cpSync(path.join(srcDir, 'app', 'repositories.gradle'), path.join(projectPath, 'app', 'repositories.gradle'));
fs.cpSync(path.join(srcDir, 'repositories.gradle'), path.join(projectPath, 'repositories.gradle'));

copyGradleTools(projectPath);
}
Expand All @@ -128,9 +128,9 @@ function copyScripts (projectPath) {
const srcScriptsDir = path.join(ROOT, 'templates', 'cordova');
const destScriptsDir = path.join(projectPath, 'cordova');
// Delete old scripts directory if this is an update.
fs.removeSync(destScriptsDir);
fs.rmSync(destScriptsDir, { recursive: true, force: true });
// Copy in the new ones.
fs.copySync(srcScriptsDir, destScriptsDir);
fs.cpSync(srcScriptsDir, destScriptsDir, { recursive: true });
}

/**
Expand Down Expand Up @@ -174,7 +174,7 @@ function validateProjectName (project_name) {
function copyGradleTools (projectPath) {
const srcDir = path.join(ROOT, 'templates', 'project');

fs.copySync(path.resolve(srcDir, 'tools'), path.resolve(projectPath, 'tools'));
fs.cpSync(path.resolve(srcDir, 'tools'), path.resolve(projectPath, 'tools'), { recursive: true });
}

/**
Expand Down Expand Up @@ -232,13 +232,13 @@ exports.create = function (project_path, config, options, events) {
const app_path = path.join(project_path, 'app', 'src', 'main');

// copy project template
fs.ensureDirSync(app_path);
fs.copySync(path.join(project_template_dir, 'assets'), path.join(app_path, 'assets'));
fs.copySync(path.join(project_template_dir, 'res'), path.join(app_path, 'res'));
fs.copySync(path.join(project_template_dir, 'gitignore'), path.join(project_path, '.gitignore'));
fs.mkdirSync(app_path, { recursive: true });
fs.cpSync(path.join(project_template_dir, 'assets'), path.join(app_path, 'assets'), { recursive: true });
fs.cpSync(path.join(project_template_dir, 'res'), path.join(app_path, 'res'), { recursive: true });
fs.cpSync(path.join(project_template_dir, 'gitignore'), path.join(project_path, '.gitignore'));

// Manually create directories that would be empty within the template (since git doesn't track directories).
fs.ensureDirSync(path.join(app_path, 'libs'));
fs.mkdirSync(path.join(app_path, 'libs'), { recursive: true });

// copy cordova.js, cordova.jar
exports.copyJsAndLibrary(project_path, options.link, safe_activity_name, target_api);
Expand All @@ -247,9 +247,9 @@ exports.create = function (project_path, config, options, events) {
const java_path = path.join(app_path, 'java');
const assets_path = path.join(app_path, 'assets');
const resource_path = path.join(app_path, 'res');
fs.ensureDirSync(java_path);
fs.ensureDirSync(assets_path);
fs.ensureDirSync(resource_path);
fs.mkdirSync(java_path, { recursive: true });
fs.mkdirSync(assets_path, { recursive: true });
fs.mkdirSync(resource_path, { recursive: true });

// store package name in cdv-gradle-config
const cdvGradleConfig = CordovaGradleConfigParserFactory.create(project_path);
Expand All @@ -261,8 +261,8 @@ exports.create = function (project_path, config, options, events) {
const activity_dir = path.join(java_path, packagePath);
const activity_path = path.join(activity_dir, safe_activity_name + '.java');

fs.ensureDirSync(activity_dir);
fs.copySync(path.join(project_template_dir, 'Activity.java'), activity_path);
fs.mkdirSync(activity_dir, { recursive: true });
fs.cpSync(path.join(project_template_dir, 'Activity.java'), activity_path);
utils.replaceFileContents(activity_path, /__ACTIVITY__/, safe_activity_name);
utils.replaceFileContents(path.join(app_path, 'res', 'values', 'strings.xml'), /__NAME__/, utils.escape(project_name));
utils.replaceFileContents(activity_path, /__ID__/, package_name);
Expand Down
2 changes: 1 addition & 1 deletion lib/emulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

const execa = require('execa');
const fs = require('fs-extra');
const fs = require('node:fs');
const android_versions = require('android-versions');
const path = require('node:path');
const Adb = require('./Adb');
Expand Down
2 changes: 1 addition & 1 deletion lib/env/java.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

const execa = require('execa');
const fs = require('fs-extra');
const fs = require('node:fs');
const path = require('node:path');
const glob = require('fast-glob');
const { CordovaError, events } = require('cordova-common');
Expand Down
16 changes: 8 additions & 8 deletions lib/pluginHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
*/

const fs = require('fs-extra');
const fs = require('node:fs');
const path = require('node:path');
const isPathInside = require('is-path-inside');
const events = require('cordova-common').events;
Expand Down Expand Up @@ -166,13 +166,13 @@ const handlers = {
scriptContent = 'cordova.define("' + moduleName + '", function(require, exports, module) {\n' + scriptContent + '\n});\n';

const wwwDest = path.resolve(project.www, 'plugins', plugin.id, obj.src);
fs.ensureDirSync(path.dirname(wwwDest));
fs.mkdirSync(path.dirname(wwwDest), { recursive: true });
fs.writeFileSync(wwwDest, scriptContent, 'utf-8');

if (options && options.usePlatformWww) {
// CB-11022 copy file to both directories if usePlatformWww is specified
const platformWwwDest = path.resolve(project.platformWww, 'plugins', plugin.id, obj.src);
fs.ensureDirSync(path.dirname(platformWwwDest));
fs.mkdirSync(path.dirname(platformWwwDest), { recursive: true });
fs.writeFileSync(platformWwwDest, scriptContent, 'utf-8');
}
},
Expand Down Expand Up @@ -217,11 +217,11 @@ function copyFile (plugin_dir, src, project_dir, dest, link) {
// check that dest path is located in project directory
if (!isPathInside(dest, project_dir)) { throw new CordovaError('Destination "' + dest + '" for source file "' + src + '" is located outside the project'); }

fs.ensureDirSync(path.dirname(dest));
fs.mkdirSync(path.dirname(dest), { recursive: true });
if (link) {
symlinkFileOrDirTree(src, dest);
} else {
fs.copySync(src, dest);
fs.cpSync(src, dest, { recursive: true });
}
}

Expand All @@ -235,11 +235,11 @@ function copyNewFile (plugin_dir, src, project_dir, dest, link) {

function symlinkFileOrDirTree (src, dest) {
if (fs.existsSync(dest)) {
fs.removeSync(dest);
fs.rmSync(dest, { recursive: true, force: true });
}

if (fs.statSync(src).isDirectory()) {
fs.ensureDirSync(path.dirname(dest));
fs.mkdirSync(path.dirname(dest), { recursive: true });
fs.readdirSync(src).forEach(function (entry) {
symlinkFileOrDirTree(path.join(src, entry), path.join(dest, entry));
});
Expand All @@ -249,7 +249,7 @@ function symlinkFileOrDirTree (src, dest) {
}

function removeFile (file) {
fs.removeSync(file);
fs.rmSync(file);
}

// Sometimes we want to remove some java, and prune any unnecessary empty directories
Expand Down
18 changes: 9 additions & 9 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
under the License.
*/

const fs = require('fs-extra');
const fs = require('node:fs');
const path = require('node:path');
const nopt = require('nopt');
const glob = require('fast-glob');
Expand Down Expand Up @@ -93,7 +93,7 @@ function updateUserProjectGradleConfig (project) {

// Write out changes
const projectGradleConfigPath = path.join(project.root, 'cdv-gradle-config.json');
fs.writeJSONSync(projectGradleConfigPath, projectGradleConfig, { spaces: 2 });
fs.writeFileSync(projectGradleConfigPath, JSON.stringify(projectGradleConfig, null, 2), 'utf-8');
}

function getUserGradleConfig (configXml) {
Expand Down Expand Up @@ -198,7 +198,7 @@ function updateConfigFilesFrom (sourceConfig, configMunger, locations) {

// First cleanup current config and merge project's one into own
// Overwrite platform config.xml with defaults.xml.
fs.copySync(locations.defaultConfigXml, locations.configXml);
fs.cpSync(locations.defaultConfigXml, locations.configXml);

// Then apply config changes from global munge to all config files
// in project (including project's config)
Expand Down Expand Up @@ -316,14 +316,14 @@ function updateProjectAccordingTo (platformConfig, locations) {
const newDestFile = path.join(locations.root, 'app', 'src', 'main', 'java', androidPkgName.replace(/\./g, '/'), path.basename(destFile));
if (newDestFile.toLowerCase() !== destFile.toLowerCase()) {
// If package was name changed we need to create new java with main activity in path matching new package name
fs.ensureDirSync(path.dirname(newDestFile));
fs.mkdirSync(path.dirname(newDestFile), { recursive: true });
events.emit('verbose', `copy ${destFile} to ${newDestFile}`);
fs.copySync(destFile, newDestFile);
fs.cpSync(destFile, newDestFile);
utils.replaceFileContents(newDestFile, /package [\w.]*;/, 'package ' + androidPkgName + ';');
events.emit('verbose', 'Wrote out Android package name "' + androidPkgName + '" to ' + newDestFile);
// If package was name changed we need to remove old java with main activity
events.emit('verbose', `remove ${destFile}`);
fs.removeSync(destFile);
fs.rmSync(destFile);
// remove any empty directories
let currentDir = path.dirname(destFile);
const sourcesRoot = path.resolve(locations.root, 'src');
Expand Down Expand Up @@ -536,16 +536,16 @@ function updateProjectSplashScreenIconBackgroundColor (splashIconBackgroundColor

function cleanupAndSetProjectSplashScreenImage (srcFile, destFilePath, possiblePreviousDestFilePath, cleanupOnly = false) {
if (fs.existsSync(possiblePreviousDestFilePath)) {
fs.removeSync(possiblePreviousDestFilePath);
fs.rmSync(possiblePreviousDestFilePath);
}

if (cleanupOnly && fs.existsSync(destFilePath)) {
// Also remove dest file path for cleanup even if previous was not use.
fs.removeSync(destFilePath);
fs.rmSync(destFilePath);
}

if (!cleanupOnly && srcFile && fs.existsSync(srcFile)) {
fs.copySync(srcFile, destFilePath);
fs.cpSync(srcFile, destFilePath);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

// TODO: Perhaps this should live in cordova-common?

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

Expand Down
Loading

0 comments on commit e012478

Please sign in to comment.