@amedia/kragl-utils
Advanced tools
Comparing version 1.2.0 to 1.2.1
# @amedia/kragl-utils | ||
## 1.2.1 | ||
### Patch Changes | ||
- ef21080: introduce prettier-config-base | ||
The base Prettier config is extracted to @amedia/prettier-config-base to | ||
allow it to be used independently from @amedia/prettier-config, if the | ||
feature probing functionality is not needed. | ||
This is in line with how @amedia/eslint-config works. | ||
- f687a86: enable plugin support for prettier | ||
## 1.2.0 | ||
@@ -4,0 +18,0 @@ |
215
index.js
@@ -1,208 +0,7 @@ | ||
import { readdirSync, readFileSync } from 'node:fs'; | ||
import { dirname, resolve, join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { execa, execaSync } from 'execa'; | ||
import fg from 'fast-glob'; | ||
export function readPackageUp({ cwd }) { | ||
const files = readdirSync(cwd); | ||
if (resolve(cwd) === '/') { | ||
process.exit(1); | ||
} | ||
if (!files.includes('package.json') && files.includes('.git')) { | ||
return null; | ||
} | ||
if (files.includes('package.json')) { | ||
const pkgPath = join(cwd, 'package.json'); | ||
const pkgJson = readFileSync(pkgPath); | ||
return { | ||
path: pkgPath, | ||
packageJson: JSON.parse(pkgJson), | ||
}; | ||
} | ||
const parent = dirname(cwd); | ||
return readPackageUp({ cwd: parent }); | ||
} | ||
export function getConsumingRoot() { | ||
return resolve(process.cwd()); | ||
} | ||
export function objHasKey(obj, prop) { | ||
return obj && Object.prototype.hasOwnProperty.call(obj, prop); | ||
} | ||
export function hasConfig(sources) { | ||
const { path: packagePath, packageJson } = readPackageUp({ | ||
cwd: getConsumingRoot(), | ||
}) || { path: getConsumingRoot(), packageJson: {} }; | ||
const root = dirname(packagePath); | ||
return sources.some((source) => { | ||
switch (source.type) { | ||
case 'file': { | ||
return !!fg.sync(source.pattern, { cwd: root, dot: true }).length; | ||
} | ||
case 'package.json': { | ||
return objHasKey(packageJson, source.property); | ||
} | ||
case 'dependency': { | ||
switch (source.dependencyType) { | ||
case 'dev': { | ||
return objHasKey(packageJson.devDependencies, source.dependency); | ||
} | ||
case 'peer': { | ||
return objHasKey(packageJson.peerDependencies, source.dependency); | ||
} | ||
default: { | ||
return objHasKey(packageJson.dependencies, source.dependency); | ||
} | ||
} | ||
} | ||
case 'executable': { | ||
return !!fg.sync(source.pattern, { | ||
cwd: join(root, 'node_modules', '.bin'), | ||
dot: true, | ||
basename: true, | ||
}).length; | ||
} | ||
default: { | ||
return false; | ||
} | ||
} | ||
}); | ||
} | ||
export const localBin = (exe) => { | ||
const { path: packagePath } = readPackageUp({ | ||
cwd: getConsumingRoot(), | ||
}) || { path: getConsumingRoot(), packageJson: {} }; | ||
const root = dirname(packagePath); | ||
const exes = fg.sync(exe, { | ||
cwd: join(root, 'node_modules', '.bin'), | ||
dot: true, | ||
basename: true, | ||
unique: true, | ||
absolute: true, | ||
}); | ||
return exes[0]; | ||
}; | ||
export const paths = (base) => { | ||
const dirName = dirname(fileURLToPath(base)); | ||
const CONSUMING_ROOT = getConsumingRoot(); | ||
const THIS_ROOT = join(dirName, '..'); | ||
const CONFIG_FOLDER = join(THIS_ROOT, 'config'); | ||
return { | ||
CONSUMING_ROOT, | ||
THIS_ROOT, | ||
CONFIG_FOLDER, | ||
local: (cfg) => join(CONFIG_FOLDER, cfg), | ||
}; | ||
}; | ||
export function handleError(error) { | ||
if (error.all) { | ||
console.log(error.all); | ||
} | ||
// If we don't have a message on the combined stdout/stderr stream | ||
// named 'all', we should print the real error in combination with the | ||
// debug flag, as it may indicate an underlying error with the | ||
// executed command. | ||
if (!error.all && process.env.KRAGL_DEBUG) { | ||
console.error(error); | ||
} | ||
process.exitCode = error.exitCode; | ||
} | ||
export function handlePromise(runner, task) { | ||
runner(task) | ||
.then((ok) => { | ||
if (ok && !task.quiet) { | ||
console.log(ok); | ||
} | ||
}) | ||
.catch((err) => handleError(err)); | ||
} | ||
export async function run(cmd, args, opts = { env: {} }) { | ||
const { env, ...rest } = opts; | ||
return execa(cmd, args, { | ||
all: true, | ||
preferLocal: true, | ||
env: { | ||
FORCE_COLOR: true, | ||
...env, | ||
}, | ||
...rest, | ||
}); | ||
} | ||
export function runSync(cmd, args, opts = { env: {} }) { | ||
const { env, ...rest } = opts; | ||
return execaSync(cmd, args, { | ||
all: true, | ||
preferLocal: true, | ||
env: { | ||
FORCE_COLOR: true, | ||
...env, | ||
}, | ||
...rest, | ||
}); | ||
} | ||
export function findPlugins() { | ||
const { path: packagePath } = readPackageUp({ | ||
cwd: getConsumingRoot(), | ||
}) || { path: getConsumingRoot() }; | ||
const root = dirname(packagePath); | ||
const plugins = fg.sync('kragl-*', { | ||
cwd: join(root, 'node_modules', '.bin'), | ||
dot: true, | ||
basename: true, | ||
}); | ||
return plugins.reduce((accumulator, current) => { | ||
const name = current.replace('kragl-', ''); | ||
return [ | ||
...accumulator, | ||
{ | ||
name, | ||
bin: current, | ||
desc: `[plugin] for ${name}`, | ||
}, | ||
]; | ||
}, []); | ||
} | ||
export function loadPlugins(program) { | ||
const plugins = findPlugins(); | ||
for (const plugin of plugins) { | ||
program.command(plugin.name, plugin.desc, { | ||
executableFile: localBin(plugin.bin), | ||
}); | ||
} | ||
} | ||
export async function runTask(runner, task) { | ||
return handlePromise(runner, task); | ||
} | ||
export async function runTasks(tasks) { | ||
return Promise.all( | ||
tasks.map((t) => { | ||
const [runner, task] = t; | ||
handlePromise(runner, task); | ||
}) | ||
); | ||
} | ||
export * from './lib/config.js'; | ||
export * from './lib/obj-has-key.js'; | ||
export * from './lib/run.js'; | ||
export * from './lib/tasks.js'; | ||
export * from './lib/plugins.js'; | ||
export * from './lib/pkg-up.js'; | ||
export * from './lib/bin.js'; |
{ | ||
"name": "@amedia/kragl-utils", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Utility functions for Kragl", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
260507
11
7671
18