Skip to content

Commit

Permalink
chore: expose fs apis
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Dec 12, 2023
1 parent 730dcfc commit e8742db
Show file tree
Hide file tree
Showing 49 changed files with 597 additions and 850 deletions.
76 changes: 51 additions & 25 deletions core/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,56 @@ const pkg = require('./package.json')

const libraryName = 'core'

export default {
input: `src/index.ts`,
output: [
{ file: pkg.main, name: libraryName, format: 'umd', sourcemap: true },
{ file: pkg.module, format: 'es', sourcemap: true },
],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: [],
watch: {
include: 'src/**',
export default [
{
input: `src/index.ts`,
output: [
{ file: pkg.main, name: libraryName, format: 'umd', sourcemap: true },
{ file: pkg.module, format: 'es', sourcemap: true },
],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: ['path'],
watch: {
include: 'src/**',
},
plugins: [
// Allow json resolution
json(),
// Compile TypeScript files
typescript({ useTsconfigDeclarationDir: true }),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
resolve(),

// Resolve source maps to the original source
sourceMaps(),
],
},
plugins: [
// Allow json resolution
json(),
// Compile TypeScript files
typescript({ useTsconfigDeclarationDir: true }),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
resolve(),
{
input: `src/node/index.ts`,
output: [{ file: 'dist/node/index.cjs', format: 'cjs', sourcemap: true }],
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
external: ['fs/promises', 'path', 'pacote', '@types/pacote', '@npmcli/arborist'],
watch: {
include: 'src/node/**',
},
plugins: [
// Allow json resolution
json(),
// Compile TypeScript files
typescript({ useTsconfigDeclarationDir: true }),
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
commonjs(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
resolve(),

// Resolve source maps to the original source
sourceMaps(),
],
}
// Resolve source maps to the original source
sourceMaps(),
],
},
]
22 changes: 11 additions & 11 deletions core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ export enum ExtensionRoute {
uninstallExtension = 'uninstallExtension',
}
export enum FileSystemRoute {
appendFile = 'appendFile',
copyFile = 'copyFile',
deleteFile = 'deleteFile',
exists = 'exists',
appendFileSync = 'appendFileSync',
copyFileSync = 'copyFileSync',
unlinkSync = 'unlinkSync',
existsSync = 'existsSync',
readdirSync = 'readdirSync',
mkdirSync = 'mkdirSync',
readFileSync = 'readFileSync',
rmdirSync = 'rmdirSync',
writeFileSync = 'writeFileSync',

isDirectory = 'isDirectory',
getResourcePath = 'getResourcePath',
getUserSpace = 'getUserSpace',
isDirectory = 'isDirectory',
listFiles = 'listFiles',
mkdir = 'mkdir',
readFile = 'readFile',
readLineByLine = 'readLineByLine',
rmdir = 'rmdir',
writeFile = 'writeFile',
}

export type ApiFunction = (...args: any[]) => any
Expand Down
66 changes: 22 additions & 44 deletions core/src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,64 @@
/**
* Writes data to a file at the specified path.
* @param {string} path - The path to the file.
* @param {string} data - The data to write to the file.
* @returns {Promise<any>} A Promise that resolves when the file is written successfully.
*/
const writeFile: (path: string, data: string) => Promise<any> = (path, data) =>
global.core.api?.writeFile(path, data)

/**
* Checks whether the path is a directory.
* @param path - The path to check.
* @returns {boolean} A boolean indicating whether the path is a directory.
*/
const isDirectory = (path: string): Promise<boolean> => global.core.api?.isDirectory(path)
const writeFileSync = (...args: any[]) => global.core.api?.writeFileSync(...args)

/**
* Reads the contents of a file at the specified path.
* @param {string} path - The path of the file to read.
* @returns {Promise<any>} A Promise that resolves with the contents of the file.
*/
const readFile: (path: string) => Promise<any> = (path) => global.core.api?.readFile(path)
const readFileSync = (...args: any[]) => global.core.api?.readFileSync(...args)
/**
* Check whether the file exists
* @param {string} path
* @returns {boolean} A boolean indicating whether the path is a file.
*/
const exists = (path: string): Promise<boolean> => global.core.api?.exists(path)
const existsSync = (...args: any[]) => global.core.api?.existsSync(...args)
/**
* List the directory files
* @param {string} path - The path of the directory to list files.
* @returns {Promise<any>} A Promise that resolves with the contents of the directory.
*/
const listFiles: (path: string) => Promise<any> = (path) => global.core.api?.listFiles(path)
const readdirSync = (...args: any[]) => global.core.api?.readdirSync(...args)
/**
* Creates a directory at the specified path.
* @param {string} path - The path of the directory to create.
* @returns {Promise<any>} A Promise that resolves when the directory is created successfully.
*/
const mkdir: (path: string) => Promise<any> = (path) => global.core.api?.mkdir(path)
const mkdirSync = (...args: any[]) => global.core.api?.mkdirSync(...args)

/**
* Removes a directory at the specified path.
* @param {string} path - The path of the directory to remove.
* @returns {Promise<any>} A Promise that resolves when the directory is removed successfully.
*/
const rmdir: (path: string) => Promise<any> = (path) => global.core.api?.rmdir(path)
const rmdirSync = (...args: any[]) =>
global.core.api?.rmdirSync(...args, { recursive: true, force: true })
/**
* Deletes a file from the local file system.
* @param {string} path - The path of the file to delete.
* @returns {Promise<any>} A Promise that resolves when the file is deleted.
*/
const deleteFile: (path: string) => Promise<any> = (path) => global.core.api?.deleteFile(path)
const unlinkSync = (...args: any[]) => global.core.api?.unlinkSync(...args)

/**
* Appends data to a file at the specified path.
* @param path path to the file
* @param data data to append
*/
const appendFile: (path: string, data: string) => Promise<any> = (path, data) =>
global.core.api?.appendFile(path, data)

const copyFile: (src: string, dest: string) => Promise<any> = (src, dest) =>
global.core.api?.copyFile(src, dest)
const appendFileSync = (...args: any[]) => global.core.api?.appendFileSync(...args)

/**
* Reads a file line by line.
* @param {string} path - The path of the file to read.
* @returns {Promise<any>} A promise that resolves to the lines of the file.
* Copy file sync.
*/
const readLineByLine: (path: string) => Promise<any> = (path) =>
global.core.api?.readLineByLine(path)
const copyFileSync = (...args: any[]) => global.core.api?.copyFileSync(...args)

// TODO: Export `dummy` fs functions automatically
// Currently adding these manually
export const fs = {
isDirectory,
writeFile,
readFile,
exists,
listFiles,
mkdir,
rmdir,
deleteFile,
appendFile,
readLineByLine,
copyFile,
writeFileSync,
readFileSync,
existsSync,
readdirSync,
mkdirSync,
rmdirSync,
unlinkSync,
appendFileSync,
copyFileSync,
}
Loading

0 comments on commit e8742db

Please sign in to comment.