Skip to content

Instantly share code, notes, and snippets.

View MarcoNicolodi's full-sized avatar

Marco Nicolodi MarcoNicolodi

View GitHub Profile
const withList = async ({ directory }) => await filesystem.list(directory)
const withFilter = async ({ directory, matching }) =>
await filesystem.find(directory, { matching })
const useChooseFile = finder => async ({ directory, message, matching }) => {
const candidateFiles = await finder({ directory, matching })
const promptOptions = {
name: 'chosenFile',
message,
choices: ['all', prompt.separator(), ...candidateFiles]
}
const { chosenFile } = await prompt.ask(promptOptions)
return chosenFile
}
function useChooseFile(finder) {
return function({ directory, message, matching }) {
const candidateFiles = await finder({ directory, matching })
const promptOptions = {
name: 'chosenFile',
message,
choices: ['all', prompt.separator(), ...candidateFiles]
}
const { chosenFile } = await prompt.ask(promptOptions)
return chosenFile
const listDirectory = useChooseFile(withList)
const filterInDirectory = useChooseFile(withFilter)
//usage
listDirectory({ directory: './tests' })
filterInDirectory({ directory: 'server', matching '*.Dockerfile' })
//same as
useChooseFile(withList)({ directory: './tests' })
useChooseFile(withFilter)({ directory: 'server', matching '*.Dockerfile' })
const withList = async ({ directory }) => await filesystem.list(directory)
const withFilter = async ({ directory, matching }) =>
await filesystem.find(directory, { matching })
const useChooseFile = finder => async ({ directory, message, matching }) => {
const candidateFiles = await finder({ directory, matching })
const promptConfig = {
type: 'list',
name: 'chosenFile',
const numbers = [1,2,4,5,6,9]
const isEven = (number) => number % 2 === 0
const even = numbers.filter(isEven)
@MarcoNicolodi
MarcoNicolodi / run-command.js
Last active February 9, 2019 19:59
Promisified node spawn facade
const runCommand = (command, options) => new Promise((resolve, reject) => {
const cmd = spawn(command, {
shell: true,
...options
});
cmd.stdout.on('data', data => print.info(data.toString()));
cmd.stdout.on('end', () => resolve(command));
cmd.stderr.on('data', data => print.info(data.toString()));
@MarcoNicolodi
MarcoNicolodi / run-and-print-command.js
Last active March 25, 2019 00:23
Enhance your functions with decorators
const runCommand = (command, options) => new Promise((resolve, reject) => {
const { shouldPrint, ...rest } = options;
//prints
if(shouldPrint)
print.info(command);
//and runs
const cmd = spawn(command, {
shell: true,
const runCommand = (command, options) => new Promise((resolve, reject) => {
const { shouldPrint, shouldMemoize, ...rest } = options;
//prints
if(shouldPrint)
print.info(command);
if(shouldMemoize)
memoize(command);
const withCommandLogger = func => (command, options) => {
print.info(`Running command: ${command}`)
return func(command, options)
}
export default withCommandLogger(runCommand);