Skip to content

Instantly share code, notes, and snippets.

@pchittum
Last active December 6, 2019 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchittum/7f2e293c0fbf284b47d1d5d52528854d to your computer and use it in GitHub Desktop.
Save pchittum/7f2e293c0fbf284b47d1d5d52528854d to your computer and use it in GitHub Desktop.
My crappy first attempt at writing a command line in oclif
// here filesToCreate had an array of object literals with details for file creation.
// so much SMH...instead, create an array of unresolved promises, then use Promise.all()
filesToCreate.forEach(file => {
this.log('creating file ' + file.name)
fs.writeFile(file.name, file.text)
.then(() => {
this.log(`created file ${file.name}`)
},
error => {
this.log(error)
process.exit()
})
.catch(error => console.error(error))
})
// let's be clear. I'm saving this as an example of what not to do with promises.
// more to document my own learning experience.
process path, make sure it exists,
if not, create it (provided flag is set to create path)
TODO: would be good to refactor in future
let namespacePath = ''
let isNewPath = false
fs.realpath(rawPath)
.then(pathName => {
namespacePath = pathName
})
.catch(error => {
if (flags.createPath) {
fs.mkdir(rawPath)
.then(() => {
return fs.realpath(rawPath)
.catch(error => {
this.error(error)
})
})
.then(pathName => {
namespacePath = pathName
isNewPath = true
})
.catch(error => {
this.error(error)
})
} else {
this.error(error)
}
})
this.log(namespacePath)
this.log(isNewPath)
const componentPath = `${namespacePath}/${flags.namespace}`
fs.mkdir(componentPath)
.then(() => {
this.info(`Created path ${componentPath}`)
})
.catch(error => {
this.error('Failed to create new component directory. ' + error)
})
let fullPath = ''
fs.realpath(componentPath)
.then(pathName => {
fullPath = pathName
return fs.writeFile(`${fullPath}/${name}.js`, jsText)
})
.then(() => {
// eslint-disable-next-line no-negated-condition
return (!flags.noHTML) ? fs.writeFile(`${fullPath}/${name}.html`, htmlText) : new Promise()
})
.then(() => {
return flags.useCSS ? fs.writeFile(`${fullPath}/${name}.css`, '') : new Promise()
})
.catch(error => {
this.error(error)
this.error('failed to correctly complete. clean up your shit.')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment