@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
62 lines (52 loc) • 2.02 kB
JavaScript
module.exports = new class CompletionFS {
async readdir(currentFolder, options) {
options = {
files: true,
folders: true,
fileRegex: /.*/,
folderRegex: /.*/,
...options
}
const fsp = require('node:fs/promises');
const path = require('node:path');
const match = /\/|(\\)[^\s]/.exec(currentFolder);
const separationChar = match?.[1] || match?.[0] || '/';
let folder = currentFolder || process.cwd();
let entries;
try {
entries = await fsp.readdir(folder, { withFileTypes: true });
} catch {
folder = path.dirname(folder);
entries = await fsp.readdir(folder, { withFileTypes: true });
}
const isCwd = folder === '.' || folder === process.cwd();
entries = entries
.filter(e =>
options.folders && e.isDirectory() && e.name.match(options.folderRegex)
|| options.files && e.isFile() && e.name.match(options.fileRegex))
.map(e => {
let result = isCwd
? path.relative(folder, e.name)
: (folder + separationChar + e.name).replace(/\\{2,}/g, '\\').replace(/\/{2,}/g, '/');
if (e.isDirectory()) {
result += separationChar;
}
return result;
});
if (entries.length === 0) {
entries.push(folder);
}
return entries;
}
async readTemplates(argv) {
const fsp = require('node:fs/promises');
const path = require('node:path');
const excludes = ['nodejs', 'java', ...argv];
return (await fsp.readdir(path.join(__dirname, '../../lib/init/template'), { withFileTypes: true }))
.filter(e =>
e.isDirectory()
&& !e.name.startsWith('_')
&& !excludes.includes(e.name))
.map(e => e.name);
}
}