Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add missing nodejs shortcuts to cds.utils #256

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apis/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type * as fs from 'node:fs'
import type * as nodePath from 'node:path'
import type * as nodeUtil from 'node:util'

/**
* Provides a set of utility functions
Expand Down Expand Up @@ -87,4 +89,19 @@ declare const utils: {
* @see https://cap.cloud.sap/docs/node.js/cds-utils#async-rm-path
*/
rm: (...path: string[]) => Promise<ReturnType<typeof fs.promises.rm>>,

/**
* Shortcut for require("fs")
*/
fs: typeof fs,

/**
* Shortcut for require("path")
*/
path: typeof nodePath,

/**
* Shortcut for require("util").inspect
*/
inspect: typeof nodeUtil.inspect,
}
34 changes: 22 additions & 12 deletions test/typescript/apis/project/cds-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,46 @@ import cds from '@sap/cds'
const uuid: string = cds.utils.uuid()
const decodeURI: string = cds.utils.decodeURI('https://developer.mozilla.org/docs/JavaScript%3A%20a_scripting_language')
const decodeComponent: string = cds.utils.decodeURIComponent('JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B')
const local: string =cds.utils.local('myFile.json')
const local: string = cds.utils.local('myFile.json')
const result: boolean = cds.utils.exists('server.js')
const isDir: string = cds.utils.isdir('app')
const isFile: string = cds.utils.isfile('package.json')

const { read, write } = cds.utils

const rd: Buffer | {} = await read ('package.json')
const rd: Buffer | {} = await read('package.json')

await write ({foo:'bar'}) .to ('some','file.json')
await write ({foo:'bar'}) .to ('some','file.json', 'pp')
await write ('some/file.json', {foo:'bar'})
await write({ foo: 'bar' }).to('some', 'file.json')
await write({ foo: 'bar' }).to('some', 'file.json', 'pp')
await write('some/file.json', { foo: 'bar' })

const { copy } = cds.utils
await copy('db/data').to('dist','db','data')
await copy('db/data').to('dist', 'db', 'data')
await copy('db/data').to('dist/db/data')
await copy('db/data','dist/db/data')
await copy('db/data', 'dist/db/data')

const { mkdirp } = cds.utils
const path :string = await mkdirp('dist','db','data')
const path1 :string = await mkdirp('dist/db/data')
const path: string = await mkdirp('dist', 'db', 'data')
const path1: string = await mkdirp('dist/db/data')

const { rmdir } = cds.utils
await rmdir('dist','db','data')
await rmdir('dist', 'db', 'data')
await rmdir('dist/db/data')

const { rimraf } = cds.utils
await rimraf('dist','db','data')
await rimraf('dist', 'db', 'data')
await rimraf('dist/db/data')

const { rm } = cds.utils
await rm('dist','db','data')
await rm('dist', 'db', 'data')
await rm('dist/db/data')

const { fs } = cds.utils
fs.writeFileSync('test', 'content', { encoding: 'utf-8' })

const { path: nodePath } = cds.utils
nodePath.resolve('dist', 'db')
nodePath.join(__dirname, 'dist', 'db')

const { inspect } = cds.utils
inspect({}, true, 10, true)