From 0a0dd1abf2d0d2d869b56db2a52495eec86b44e1 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Wed, 20 Nov 2024 10:52:33 +0100 Subject: [PATCH] refactor(cli): remove usage of @ionic/utils-fs (#7762) --- cli/package.json | 3 ++- cli/src/android/add.ts | 2 +- cli/src/android/common.ts | 2 +- cli/src/android/doctor.ts | 3 ++- cli/src/android/open.ts | 2 +- cli/src/android/update.ts | 4 ++-- cli/src/common.ts | 2 +- cli/src/config.ts | 2 +- cli/src/cordova.ts | 2 +- cli/src/ios/build.ts | 2 +- cli/src/ios/common.ts | 2 +- cli/src/ios/update.ts | 2 +- cli/src/ipc.ts | 2 +- cli/src/plugin.ts | 2 +- cli/src/sysconfig.ts | 2 +- cli/src/tasks/add.ts | 2 +- cli/src/tasks/copy.ts | 2 +- cli/src/tasks/doctor.ts | 2 +- cli/src/tasks/migrate.ts | 2 +- cli/src/tasks/sourcemaps.ts | 2 +- cli/src/util/fs.ts | 35 ++++++++++++++++++++++++++++++++++- cli/src/util/iosplugin.ts | 6 ++++-- cli/src/util/livereload.ts | 2 +- cli/src/util/node.ts | 2 +- cli/src/util/spm.ts | 2 +- cli/src/util/template.ts | 2 +- cli/src/util/xml.ts | 2 +- cli/test/init.spec.ts | 2 +- cli/test/util.ts | 2 +- 29 files changed, 68 insertions(+), 31 deletions(-) diff --git a/cli/package.json b/cli/package.json index 3e9acfdb2..f20151812 100644 --- a/cli/package.json +++ b/cli/package.json @@ -45,12 +45,12 @@ }, "dependencies": { "@ionic/cli-framework-output": "^2.2.5", - "@ionic/utils-fs": "^3.1.6", "@ionic/utils-subprocess": "2.1.11", "@ionic/utils-terminal": "^2.3.3", "commander": "^9.3.0", "debug": "^4.3.4", "env-paths": "^2.2.0", + "fs-extra": "^11.2.0", "kleur": "^4.1.4", "native-run": "^2.0.0", "open": "^8.4.0", @@ -64,6 +64,7 @@ }, "devDependencies": { "@types/debug": "^4.1.7", + "@types/fs-extra": "^11.0.4", "@types/jest": "^29.5.0", "@types/plist": "^3.0.2", "@types/prompts": "^2.0.14", diff --git a/cli/src/android/add.ts b/cli/src/android/add.ts index d4a85960e..968d5ee41 100644 --- a/cli/src/android/add.ts +++ b/cli/src/android/add.ts @@ -1,4 +1,4 @@ -import { pathExists, writeFile } from '@ionic/utils-fs'; +import { pathExists, writeFile } from 'fs-extra'; import { homedir } from 'os'; import { join } from 'path'; diff --git a/cli/src/android/common.ts b/cli/src/android/common.ts index 41aef222d..162b3e563 100644 --- a/cli/src/android/common.ts +++ b/cli/src/android/common.ts @@ -1,4 +1,4 @@ -import { copy, remove, mkdirp, readFile, pathExists, writeFile } from '@ionic/utils-fs'; +import { copy, remove, mkdirp, readFile, pathExists, writeFile } from 'fs-extra'; import { join, resolve } from 'path'; import { checkCapacitorPlatform } from '../common'; diff --git a/cli/src/android/doctor.ts b/cli/src/android/doctor.ts index 2ea3fe7d2..b5de5ed62 100644 --- a/cli/src/android/doctor.ts +++ b/cli/src/android/doctor.ts @@ -1,4 +1,4 @@ -import { pathExists, readdirp, readFile } from '@ionic/utils-fs'; +import { pathExists, readFile } from 'fs-extra'; import { join, extname, parse } from 'path'; import c from '../colors'; @@ -6,6 +6,7 @@ import { check } from '../common'; import type { Config } from '../definitions'; import { fatal, isFatal } from '../errors'; import { logSuccess } from '../log'; +import { readdirp } from '../util/fs'; import { readXML } from '../util/xml'; export async function doctorAndroid(config: Config): Promise { diff --git a/cli/src/android/open.ts b/cli/src/android/open.ts index 50bbed3b4..c45a1f9a3 100644 --- a/cli/src/android/open.ts +++ b/cli/src/android/open.ts @@ -1,5 +1,5 @@ -import { pathExists } from '@ionic/utils-fs'; import Debug from 'debug'; +import { pathExists } from 'fs-extra'; import open from 'open'; import c from '../colors'; diff --git a/cli/src/android/update.ts b/cli/src/android/update.ts index 3e89d1b6b..940710503 100644 --- a/cli/src/android/update.ts +++ b/cli/src/android/update.ts @@ -1,5 +1,5 @@ -import { copy, remove, pathExists, readdirp, readFile, writeFile, writeJSON } from '@ionic/utils-fs'; import Debug from 'debug'; +import { copy, remove, pathExists, readFile, writeFile, writeJSON } from 'fs-extra'; import { dirname, extname, join, relative, resolve } from 'path'; import c from '../colors'; @@ -20,7 +20,7 @@ import { import type { Plugin } from '../plugin'; import { copy as copyTask } from '../tasks/copy'; import { patchOldCapacitorPlugins } from '../tasks/migrate'; -import { convertToUnixPath } from '../util/fs'; +import { readdirp, convertToUnixPath } from '../util/fs'; import { resolveNode } from '../util/node'; import { extractTemplate } from '../util/template'; diff --git a/cli/src/common.ts b/cli/src/common.ts index fec3d0fc1..f21e56678 100644 --- a/cli/src/common.ts +++ b/cli/src/common.ts @@ -1,5 +1,5 @@ -import { readJSON, pathExists } from '@ionic/utils-fs'; import { prettyPath } from '@ionic/utils-terminal'; +import { readJSON, pathExists } from 'fs-extra'; import { dirname, join } from 'path'; import c from './colors'; diff --git a/cli/src/config.ts b/cli/src/config.ts index c88590185..e504f2539 100644 --- a/cli/src/config.ts +++ b/cli/src/config.ts @@ -1,5 +1,5 @@ -import { pathExists, readFile, readJSON, writeFile, writeJSON } from '@ionic/utils-fs'; import Debug from 'debug'; +import { pathExists, readFile, readJSON, writeFile, writeJSON } from 'fs-extra'; import { dirname, extname, join, relative, resolve } from 'path'; import c from './colors'; diff --git a/cli/src/cordova.ts b/cli/src/cordova.ts index 31f05221c..a67f4419c 100644 --- a/cli/src/cordova.ts +++ b/cli/src/cordova.ts @@ -1,4 +1,4 @@ -import { copy, ensureDir, mkdirp, pathExists, readFile, remove, writeFile } from '@ionic/utils-fs'; +import { copy, ensureDir, mkdirp, pathExists, readFile, remove, writeFile } from 'fs-extra'; import { basename, extname, join, resolve } from 'path'; import plist from 'plist'; import type { PlistObject } from 'plist'; diff --git a/cli/src/ios/build.ts b/cli/src/ios/build.ts index 838a4b451..6c88421d3 100644 --- a/cli/src/ios/build.ts +++ b/cli/src/ios/build.ts @@ -1,4 +1,4 @@ -import { writeFileSync, unlinkSync } from '@ionic/utils-fs'; +import { writeFileSync, unlinkSync } from 'fs-extra'; import { basename, join } from 'path'; import rimraf from 'rimraf'; diff --git a/cli/src/ios/common.ts b/cli/src/ios/common.ts index 925ee64a4..60036250b 100644 --- a/cli/src/ios/common.ts +++ b/cli/src/ios/common.ts @@ -1,5 +1,5 @@ -import { readFile, writeFile } from '@ionic/utils-fs'; import { execSync } from 'child_process'; +import { readFile, writeFile } from 'fs-extra'; import { resolve } from 'path'; import c from '../colors'; diff --git a/cli/src/ios/update.ts b/cli/src/ios/update.ts index 3345bfcc4..83724ebe9 100644 --- a/cli/src/ios/update.ts +++ b/cli/src/ios/update.ts @@ -1,4 +1,4 @@ -import { copy, remove, pathExists, readFile, realpath, writeFile } from '@ionic/utils-fs'; +import { copy, remove, pathExists, readFile, realpath, writeFile } from 'fs-extra'; import { basename, dirname, join, relative } from 'path'; import c from '../colors'; diff --git a/cli/src/ipc.ts b/cli/src/ipc.ts index 4bf1668ff..41894e53a 100644 --- a/cli/src/ipc.ts +++ b/cli/src/ipc.ts @@ -1,6 +1,6 @@ -import { open, mkdirp } from '@ionic/utils-fs'; import { fork } from '@ionic/utils-subprocess'; import Debug from 'debug'; +import { open, mkdirp } from 'fs-extra'; import { request } from 'https'; import { resolve } from 'path'; diff --git a/cli/src/plugin.ts b/cli/src/plugin.ts index b5b98c996..884bba998 100644 --- a/cli/src/plugin.ts +++ b/cli/src/plugin.ts @@ -1,4 +1,4 @@ -import { readJSON } from '@ionic/utils-fs'; +import { readJSON } from 'fs-extra'; import { dirname, join } from 'path'; import c from './colors'; diff --git a/cli/src/sysconfig.ts b/cli/src/sysconfig.ts index 03abb0e0d..487095404 100644 --- a/cli/src/sysconfig.ts +++ b/cli/src/sysconfig.ts @@ -1,5 +1,5 @@ -import { readJSON, writeJSON, mkdirp } from '@ionic/utils-fs'; import Debug from 'debug'; +import { readJSON, writeJSON, mkdirp } from 'fs-extra'; import { dirname, resolve } from 'path'; import { ENV_PATHS } from './util/cli'; diff --git a/cli/src/tasks/add.ts b/cli/src/tasks/add.ts index 5227650d4..f849132bf 100644 --- a/cli/src/tasks/add.ts +++ b/cli/src/tasks/add.ts @@ -1,5 +1,5 @@ -import { pathExists } from '@ionic/utils-fs'; import { prettyPath } from '@ionic/utils-terminal'; +import { pathExists } from 'fs-extra'; import { addAndroid, createLocalProperties } from '../android/add'; import { editProjectSettingsAndroid, checkAndroidPackage } from '../android/common'; diff --git a/cli/src/tasks/copy.ts b/cli/src/tasks/copy.ts index bb1f4f2f7..c59028279 100644 --- a/cli/src/tasks/copy.ts +++ b/cli/src/tasks/copy.ts @@ -1,4 +1,4 @@ -import { copy as fsCopy, pathExists, remove, writeJSON } from '@ionic/utils-fs'; +import { copy as fsCopy, pathExists, remove, writeJSON } from 'fs-extra'; import { basename, join, relative, resolve } from 'path'; import c from '../colors'; diff --git a/cli/src/tasks/doctor.ts b/cli/src/tasks/doctor.ts index 67cd64099..eb357bd0a 100644 --- a/cli/src/tasks/doctor.ts +++ b/cli/src/tasks/doctor.ts @@ -1,4 +1,4 @@ -import { readJSON } from '@ionic/utils-fs'; +import { readJSON } from 'fs-extra'; import { doctorAndroid } from '../android/doctor'; import c from '../colors'; diff --git a/cli/src/tasks/migrate.ts b/cli/src/tasks/migrate.ts index b6fc89565..089e167d1 100644 --- a/cli/src/tasks/migrate.ts +++ b/cli/src/tasks/migrate.ts @@ -1,4 +1,4 @@ -import { writeFileSync, readFileSync, existsSync } from '@ionic/utils-fs'; +import { writeFileSync, readFileSync, existsSync } from 'fs-extra'; import { join } from 'path'; import rimraf from 'rimraf'; import { coerce, gt, gte, lt } from 'semver'; diff --git a/cli/src/tasks/sourcemaps.ts b/cli/src/tasks/sourcemaps.ts index 6ebc0fdb1..b460972f0 100644 --- a/cli/src/tasks/sourcemaps.ts +++ b/cli/src/tasks/sourcemaps.ts @@ -1,4 +1,4 @@ -import { readdirSync, existsSync, readFileSync, writeFileSync, unlinkSync, lstatSync } from '@ionic/utils-fs'; +import { readdirSync, existsSync, readFileSync, writeFileSync, unlinkSync, lstatSync } from 'fs-extra'; import { join, extname } from 'path'; import type { Config } from '../definitions'; diff --git a/cli/src/util/fs.ts b/cli/src/util/fs.ts index 60f60e194..f1613044a 100644 --- a/cli/src/util/fs.ts +++ b/cli/src/util/fs.ts @@ -1,4 +1,6 @@ -import { existsSync, lstatSync, readdirSync, rmdirSync, unlinkSync } from '@ionic/utils-fs'; +import { statSync, type Stats } from 'fs'; +import { readdir } from 'fs/promises'; +import { existsSync, lstatSync, readdirSync, rmdirSync, unlinkSync } from 'fs-extra'; import { join } from 'path'; export const convertToUnixPath = (path: string): string => { @@ -18,3 +20,34 @@ export const deleteFolderRecursive = (directoryPath: any): void => { rmdirSync(directoryPath); } }; + +export interface ReaddirPOptions { + /** + * Filter out items from the walk process from the final result. + * + * @return `true` to keep, otherwise the item is filtered out + */ + readonly filter: (item: WalkerItem) => boolean; +} + +export interface WalkerItem { + path: string; + stats: Stats; +} + +export async function readdirp(dir: string, { filter }: ReaddirPOptions): Promise { + const dirContent = await readdir(dir, { recursive: true }); + const dirContentWalker: WalkerItem[] = []; + const filteredContent: string[] = []; + dirContent.forEach((element) => { + const path = join(dir, element); + const stats = statSync(path); + dirContentWalker.push({ path, stats }); + }); + dirContentWalker.forEach((element) => { + if (filter(element)) { + filteredContent.push(element.path); + } + }); + return filteredContent; +} diff --git a/cli/src/util/iosplugin.ts b/cli/src/util/iosplugin.ts index 33bd2829b..8ce42b35e 100644 --- a/cli/src/util/iosplugin.ts +++ b/cli/src/util/iosplugin.ts @@ -1,5 +1,4 @@ -import type { ReaddirPOptions } from '@ionic/utils-fs'; -import { readFileSync, readdirp, readJSONSync, writeJSONSync } from '@ionic/utils-fs'; +import { readFileSync, readJSONSync, writeJSONSync } from 'fs-extra'; import { resolve } from 'path'; import { getCordovaPlugins } from '../cordova'; @@ -7,6 +6,9 @@ import type { Config } from '../definitions'; import type { Plugin } from '../plugin'; import { getPluginType, PluginType } from '../plugin'; +import type { ReaddirPOptions } from './fs'; +import { readdirp } from './fs'; + export async function getPluginFiles(plugins: Plugin[]): Promise { let filenameList: string[] = []; diff --git a/cli/src/util/livereload.ts b/cli/src/util/livereload.ts index 6aed863a4..c3b15ee9f 100644 --- a/cli/src/util/livereload.ts +++ b/cli/src/util/livereload.ts @@ -1,4 +1,4 @@ -import { readJSONSync, writeJSONSync } from '@ionic/utils-fs'; +import { readJSONSync, writeJSONSync } from 'fs-extra'; import { networkInterfaces } from 'os'; import { join } from 'path'; diff --git a/cli/src/util/node.ts b/cli/src/util/node.ts index 34e2adf16..299dffdd3 100644 --- a/cli/src/util/node.ts +++ b/cli/src/util/node.ts @@ -1,5 +1,5 @@ -import { readFileSync } from '@ionic/utils-fs'; import { existsSync } from 'fs'; +import { readFileSync } from 'fs-extra'; import { resolve } from 'path'; import type typescript from 'typescript'; diff --git a/cli/src/util/spm.ts b/cli/src/util/spm.ts index 60ef128b1..6ffb73364 100644 --- a/cli/src/util/spm.ts +++ b/cli/src/util/spm.ts @@ -1,4 +1,4 @@ -import { existsSync, readFileSync, writeFileSync } from '@ionic/utils-fs'; +import { existsSync, readFileSync, writeFileSync } from 'fs-extra'; import { join, relative, resolve } from 'path'; import type { Config } from '../definitions'; diff --git a/cli/src/util/template.ts b/cli/src/util/template.ts index f2000560e..76b699ee2 100644 --- a/cli/src/util/template.ts +++ b/cli/src/util/template.ts @@ -1,4 +1,4 @@ -import { mkdirp } from '@ionic/utils-fs'; +import { mkdirp } from 'fs-extra'; import tar from 'tar'; export async function extractTemplate(src: string, dir: string): Promise { diff --git a/cli/src/util/xml.ts b/cli/src/util/xml.ts index 3425bbfb1..8a635c725 100644 --- a/cli/src/util/xml.ts +++ b/cli/src/util/xml.ts @@ -1,4 +1,4 @@ -import { readFile } from '@ionic/utils-fs'; +import { readFile } from 'fs-extra'; import xml2js from 'xml2js'; export async function readXML(path: string): Promise { diff --git a/cli/test/init.spec.ts b/cli/test/init.spec.ts index d8423d7d3..14be69d64 100644 --- a/cli/test/init.spec.ts +++ b/cli/test/init.spec.ts @@ -1,4 +1,4 @@ -import { mkdir } from '@ionic/utils-fs'; +import { mkdir } from 'fs-extra'; import { join } from 'path'; import { APP_ID, APP_NAME, run, mktmp, MappedFS } from './util'; diff --git a/cli/test/util.ts b/cli/test/util.ts index 6f605d776..435515220 100644 --- a/cli/test/util.ts +++ b/cli/test/util.ts @@ -1,5 +1,5 @@ -import { mkdir, mkdirp, readFile, pathExists, writeFile } from '@ionic/utils-fs'; import { exec } from 'child_process'; +import { mkdir, mkdirp, readFile, pathExists, writeFile } from 'fs-extra'; import { join, resolve } from 'path'; import tmp from 'tmp'; import type { DirCallback } from 'tmp';