@swell/cli
Version:
Swell's command line interface/utility
105 lines (104 loc) • 3.66 kB
JavaScript
import { globby, globbySync, isGitIgnored, } from 'globby';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { AllConfigPaths, ConfigType, StandardConfigPaths, StorefrontConfigPaths, ThemeConfigPaths, filePathExists, } from './index.js';
function globOptions(appPath, options) {
return {
cwd: appPath,
dot: true,
gitignore: true,
...options,
expandDirectories: true,
// Explicitly ignore node_modules and lock files just in case
ignore: [
'**/.swellrc',
'**/.git/**',
'**/node_modules/**',
'**/package-lock.json',
'**/yarn.lock',
...(options?.ignore || []),
],
};
}
function globFilesSync(pattern, appPath, dirPath = '.', options = {}) {
return globbySync(`${dirPath}${path.sep}${pattern}`, globOptions(appPath, options));
}
function globFiles(pattern, appPath, dirPath = '.', options = {}) {
return globby(`${dirPath}${path.sep}${pattern}`, globOptions(appPath, options));
}
export function globAllFilesByPath(appPath, dirPath) {
return globFiles('**', appPath, dirPath);
}
export function getGlobIgnorePathsChecker(appPath) {
return isGitIgnored({ cwd: appPath });
}
function getConfigDirsPaths(appPath) {
const configDirsPaths = [];
for (const [type, configType] of Object.entries(ConfigType)) {
const configPath = AllConfigPaths[type];
if (!configPath) {
continue;
}
const configDir = path.join(appPath, configPath);
if (filePathExists(configDir)) {
configDirsPaths.push({ configDir: configPath, configType });
}
}
return configDirsPaths;
}
export function* allConfigDirsPaths(appPath) {
for (const { configDir, configType } of getConfigDirsPaths(appPath)) {
yield { configDir, configType };
}
}
export function* allConfigFilesPaths(appPath) {
for (const { configDir, configType } of allConfigDirsPaths(appPath)) {
yield* allConfigFilesInDir(appPath, configDir, configType);
}
}
export function* allConfigFilesPathsByType(appPath, configTypeKey) {
const configPath = AllConfigPaths[configTypeKey];
yield* allConfigFilesInDir(appPath, configPath, ConfigType[configTypeKey]);
}
export function* allConfigFilesInDir(appPath, dirPath, configType) {
const filePaths = globFilesSync('**', appPath, dirPath);
for (const configFile of filePaths) {
yield { configDir: dirPath, configFile, configType };
}
}
export function* allBaseFilesInDir(appPath) {
// Only get base files excluding config directories
const configPaths = getConfigDirsPaths(appPath);
const baseFiles = globFilesSync('**', appPath, '.', {
ignore: configPaths.map((config) => `${config.configDir}/**`),
});
for (const path of baseFiles) {
yield path;
}
}
// this function is used to create folders for app
export function getAllConfigPaths(appType) {
const configPaths = [];
const ConfigPathsByType = appType === 'storefront'
? StorefrontConfigPaths
: appType === 'theme'
? ThemeConfigPaths
: StandardConfigPaths;
for (const type of Object.keys(ConfigPathsByType)) {
if (Object.hasOwn(AllConfigPaths, type)) {
const configPath = AllConfigPaths[type];
configPaths.push(configPath);
}
}
return configPaths;
}
export function isPathDirectory(path) {
try {
const stat = fs.statSync(path);
return stat.isDirectory();
}
catch {
// Handle error (e.g., file not found)
return false;
}
}