Skip to content

Commit

Permalink
Switch to pascal case for filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
dpikt committed Nov 17, 2017
1 parent b30f3ff commit 4f22661
Show file tree
Hide file tree
Showing 9 changed files with 1,789 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: '@launchpadlab/eslint-config/es6'
}
40 changes: 27 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,44 @@
const fs = require('fs-extra')
const glob = require('glob')
const camelCase = require('lodash.camelcase')
const kebabCase = require('dashify')
const path = require('path')

function generate (subSectionName, destination) {
const packagePath = path.resolve(require.resolve('@launchpadlab/lp-subsection-generator'), '../')
// Compute all casings
const camelCaseName = camelCase(subSectionName)
const kebabCaseName = kebabCase(subSectionName)
const pascalCaseName = pascalCase(subSectionName)
// Compute paths
const packagePath = getPackagePath()
const root = process.cwd()
// GENERATE
console.log('Generating...')
// Copy files over
fs.copySync(path.resolve(packagePath, './template'), path.resolve(packagePath, `./${ subSectionName }`))
fs.copySync(path.resolve(packagePath, './template'), path.resolve(packagePath, `./${ camelCaseName }`))
// Replace template variables in files
const fileNames = glob.sync(path.resolve(packagePath, `./${ subSectionName }/**/*.js`))
fileNames.map(fileName => populateTemplateFile(fileName, subSectionName))
const allFiles = glob.sync(path.resolve(packagePath, `./${ camelCaseName }/**/*.js`))
allFiles.forEach(file => {
const template = fs.readFileSync(file, 'utf8')
const result = template
.replace(/%sub-section%/g, kebabCaseName)
.replace(/%SubSection%/g, pascalCaseName)
return fs.writeFileSync(file, result)
})
// Rename view file
fs.renameSync(path.resolve(packagePath, `./${ subSectionName }/views/sub-section.js`), path.resolve(packagePath, `./${ subSectionName }/views/${ subSectionName }.js`))
fs.renameSync(path.resolve(packagePath, `./${ camelCaseName }/views/SubSection.js`), path.resolve(packagePath, `./${ camelCaseName }/views/${ pascalCaseName }.js`))
// Move to final dest
fs.moveSync(path.resolve(packagePath, `./${ subSectionName }`), path.resolve(root, destination, subSectionName))
fs.moveSync(path.resolve(packagePath, `./${ camelCaseName }`), path.resolve(root, destination, camelCaseName))
console.log('Done!')
}

function populateTemplateFile (fileName, kebabCaseName) {
const file = fs.readFileSync(fileName, 'utf8')
const result = file
.replace(/%sub-section%/g, kebabCaseName)
.replace(/%SubSection%/g, pascalCase(kebabCaseName))
return fs.writeFileSync(fileName, result)
function getPackagePath () {
try {
return path.resolve(require.resolve('@launchpadlab/lp-subsection-generator'), '../')
} catch (e) {
console.log('Path not found, running locally')
return __dirname
}
}

function pascalCase (str) {
Expand All @@ -41,4 +55,4 @@ function main () {
return generate(subSectionName, destination)
}

main()
if (!module.parent) main()
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
"main": "index.js",
"scripts": {
"start": "node ./index.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint index.js"
},
"bin": {
"generate-subsection": "./index.js"
},
"author": "dpikt",
"license": "ISC",
"dependencies": {
"dashify": "^0.2.2",
"fs-extra": "^4.0.2",
"glob": "^7.1.2",
"lodash.camelcase": "^4.3.0"
},
"devDependencies": {
"@launchpadlab/eslint-config": "^2.1.0",
"eslint": "^4.11.0"
}
}
Empty file removed template/components/index.js
Empty file.
Empty file removed template/forms/index.js
Empty file.
1 change: 1 addition & 0 deletions template/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import { Route, IndexRoute } from 'react-router'
import * as Views from './views'
import Layout from './Layout'

const Routes = (
<Route path="%sub-section%" component={ Layout }>
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion template/views/index.js

This file was deleted.

Loading

0 comments on commit 4f22661

Please sign in to comment.