Skip to content

Commit

Permalink
Transition to more explicit module configs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan-Schwartz committed Mar 18, 2017
1 parent 928522e commit 377103f
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 192 deletions.
24 changes: 17 additions & 7 deletions commands/add-module.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var fs = require('fs')
var Path = require('path')
var semver = require('semver')
var semverIntersect = require('semver-set').intersect
Expand All @@ -19,20 +18,31 @@ module.exports = function addModule (vfs, baseConfig, moduleName, moduleArgs) {
throw new errors.ModuleError(`Module ${colors.subject(moduleName)} is already a part of this project.`)
}

try {
fs.accessSync($(`modules/${moduleName}`))
var config = require(`../modules/${moduleName}/config`);

if ( moduleArgs.length > config.maxCLIArgs ) {
throw new errors.TooManyArguments(moduleName, config.maxCLIArgs)
}
catch (e) {
throw new Error(`pult: Module '${moduleName}' does not exist.`)

for (var conflict of config.pultModuleConflicts) {
if ( baseConfig.package.addedPultModules.includes(conflict) ) {
throw new errors.IncompatibleModule(moduleName, conflict)
}
}

var config = require(`../modules/${moduleName}/config`)
var moduleConfig = config(vfs, baseConfig, moduleArgs)
for (var dep of config.pultModuleDeps) {
if ( ! baseConfig.package.addedPultModules.includes(dep) ) {
throw new errors.MissingDependency(dep)
}
}

var moduleConfig = config.get(vfs, baseConfig, moduleArgs)

var newPackage = Object.assign({}, baseConfig.package, moduleConfig.package || {}, {
addedPultModules: addedModules.concat([moduleName])
})


if ( moduleConfig.dependencies || moduleConfig.devDependencies ) {
// package-merge wants strings :(
Object.assign(newPackage, {
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var errors = require('./lib/errors')
var exec = require('./lib/exec')
var colors = require('./lib/colors')

var fs = require('fs')
var Path = require('path')
var program = require('commander')

Expand Down Expand Up @@ -34,6 +35,13 @@ program
// Wrap everything in co to easily catch errors
co(function * () {

try {
fs.accessSync( Path.resolve(process.cwd(), `..`, `modules/${module}/config.js` ) )
}
catch (e) {
throw new errors.NonexistentModule(module)
}

var config = {
package: require( Path.resolve(process.cwd(), 'package.json')),
projectRoot: process.cwd(),
Expand Down
21 changes: 20 additions & 1 deletion lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ exports.ModuleError = class ModuleError extends exports.PultError {}
exports.MissingDependency = class MissingDependency extends exports.PultError {
constructor(dep) {
super(`Missing dependency ${ c.subject(dep) }. Try adding it first:\n\n $ pult add ${dep}`)
this.dependency = dep
}
}

exports.IncompatibleModule = class IncompatibleDependency extends exports.PultError {
constructor(dep, conflict) {
super(`Module ${ c.subject(dep) } is incompatible with the module ${c.subject(conflict)}`)
}
}

exports.TooManyArguments = class TooManyArguments extends exports.PultError {
constructor(name, max) {
// 0 argumentS, 1 argument, 2 argumentS (English is the worst)
const argumentPlurality = max === 1 ? '' : 's';
super(`Module ${ c.subject(name) } only allows ${ c.error(max) } argument${argumentPlurality}`)
}
}

exports.NonexistentModule = class NonexistentDependency extends exports.PultError {
constructor(name) {
super(`Module '${c.subject(name)}' does not exist.`)
}
}
13 changes: 9 additions & 4 deletions modules/api/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
var lib = require('../../lib')


module.exports = function configKnex (vfs, baseConfig, moduleArgs) {
module.exports = {
maxCLIArgs: 0,
pultModuleDeps: [],
pultModuleConflicts: [],
get: function get (vfs, baseConfig, moduleArgs) {

var serverConfig = lib.clone(baseConfig.server)
var serverConfig = lib.clone(baseConfig.server)

serverConfig.routerPipeline.unshift('./mount-api.js')
serverConfig.routerPipeline.unshift('./mount-api.js')

return { server: serverConfig }
return { server: serverConfig }
}
}
30 changes: 16 additions & 14 deletions modules/cookie-session/config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
var lib = require('../../lib')


module.exports = function configCookieSession (vfs, baseConfig, moduleArgs) {
if ( moduleArgs.length >= 2 ) {
throw new lib.errors.ModuleError('Module `cookie-sessions` only takes 1 argument')
}
module.exports = {
maxCLIArgs: 0,
pultModuleDeps: [],
pultModuleConflicts: [],
get: function get (vfs, baseConfig, moduleArgs) {

var serverConfig = lib.clone(baseConfig.server)
var serverConfig = lib.clone(baseConfig.server)

serverConfig.routerPipeline.push('./cookie-sessions.js')
serverConfig.routerPipeline.push('./cookie-sessions.js')

var config = {
dependencies: {
"cookie-session": "*",
},
server: serverConfig,
}
var config = {
dependencies: {
"cookie-session": "*",
},
server: serverConfig,
}

config.installs = Object.keys(config.dependencies)
config.installs = Object.keys(config.dependencies)

return config
return config
}
}
12 changes: 6 additions & 6 deletions modules/knex-model/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var lib = require('../../lib')


module.exports = function configKnex (vfs, baseConfig, moduleArgs) {

if ( ! baseConfig.package.addedPultModules.includes('knex') ) {
throw new lib.errors.MissingDependency('knex')
module.exports = {
maxCLIArgs: 0,
pultModuleDeps: [ 'knex' ],
pultModuleConflicts: [],
get: function get (vfs, baseConfig, moduleArgs) {
return {}
}

return {}
}
45 changes: 23 additions & 22 deletions modules/knex/config.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
var lib = require('../../lib')

module.exports = {
maxCLIArgs: 1,
pultModuleDeps: [],
pultModuleConflicts: [],
get: function get (vfs, baseConfig, moduleArgs) {

module.exports = function configKnex (vfs, baseConfig, moduleArgs) {
if ( moduleArgs.length >= 2 ) {
throw new lib.errors.ModuleError('Module `knex` only takes 1 argument')
}

var dialect = dialectMap[ moduleArgs[0] ]
if ( ! dialect ) {
let options = Object.keys(driverMap).sort()
throw new lib.errors.ModuleError(`Module ${
lib.c.subject('knex')
} needs a dialect. Options are:\n\n ${
options.join(', ')
}\n\nUpon choosing, re-run the command like this:\n\n $ pult add knex postgres`)
}
var dialect = dialectMap[ moduleArgs[0] ]
if ( ! dialect ) {
let options = Object.keys(driverMap).sort()
throw new lib.errors.ModuleError(`Module ${
lib.c.subject('knex')
} needs a dialect. Options are:\n\n ${
options.join(', ')
}\n\nUpon choosing, re-run the command like this:\n\n $ pult add knex postgres`)
}

var config = {
var config = {

client: driverMap[dialect],
client: driverMap[dialect],

dependencies: {
knex: '^0.12.5',
[driverMap[dialect]]: '*',
dependencies: {
knex: '^0.12.5',
[driverMap[dialect]]: '*',
}
}
}

config.installs = Object.keys(config.dependencies)
config.installs = Object.keys(config.dependencies)

return config
return config
}
}

// Taken from https://github.com/tgriesser/knex/tree/3609de76b34cb9ed6171505f3b614c0526c0e9ca/src/dialects
Expand Down
64 changes: 33 additions & 31 deletions modules/less/config.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
var lib = require('../../lib')


module.exports = function configClient (vfs, baseConfig, moduleArgs) {
if ( moduleArgs.length >= 2 ) {
throw new lib.errors.ModuleError('Module `less` only takes 1 argument')
}
module.exports = {
maxCLIArgs: 0,
pultModuleDeps: [],
pultModuleConflicts: [],
get: function get (vfs, baseConfig, moduleArgs) {

var serverConfig = lib.clone(baseConfig.server)
var serverConfig = lib.clone(baseConfig.server)

serverConfig.routerPipeline.unshift('./style-bundles.js')
serverConfig.routerPipeline.unshift('./style-bundles.js')


//
// Inject link tag into main html file
//
var isSpa = baseConfig.package.addedPultModules.includes('spa')
var isMarko = baseConfig.package.addedPultModules.includes('marko')
//
// Inject link tag into main html file
//
var isSpa = baseConfig.package.addedPultModules.includes('spa')
var isMarko = baseConfig.package.addedPultModules.includes('marko')

if ( isSpa || isMarko ) {
var htmlPath = baseConfig.projectRoot + (
isSpa ? '/client/public/index.html' : '/client/pages/_layouts/main.marko'
)
if ( isSpa || isMarko ) {
var htmlPath = baseConfig.projectRoot + (
isSpa ? '/client/public/index.html' : '/client/pages/_layouts/main.marko'
)

var html = vfs.read(htmlPath)
var html = vfs.read(htmlPath)

var spaces = html.match(/( *)<\/head>/)[1]
var result = html.replace(
'</head>',
` <link rel="stylesheet" type="text/css" href="/app-bundle.css">\n${ spaces }</head>`
)
vfs.write(htmlPath, result)
}
var spaces = html.match(/( *)<\/head>/)[1]
var result = html.replace(
'</head>',
` <link rel="stylesheet" type="text/css" href="/app-bundle.css">\n${ spaces }</head>`
)
vfs.write(htmlPath, result)
}


var config = {
dependencies: {
"node-less-endpoint": "0.x",
},
server: serverConfig,
}
var config = {
dependencies: {
"node-less-endpoint": "0.x",
},
server: serverConfig,
}

config.installs = Object.keys(config.dependencies)
config.installs = Object.keys(config.dependencies)

return config
return config
}
}
81 changes: 39 additions & 42 deletions modules/marko/config.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,44 @@
var lib = require('../../lib')


module.exports = function configClient (vfs, baseConfig, moduleArgs) {
if ( moduleArgs.length >= 2 ) {
throw new lib.errors.ModuleError('Module `marko` only takes 1 argument')
module.exports = {
maxCLIArgs: 0,
pultModuleDeps: [],
pultModuleConflicts: [ 'spa' ],
get: function get (vfs, baseConfig, moduleArgs) {

var serverConfig = lib.clone(baseConfig.server)

serverConfig.routerPipeline.unshift('./marko.js')

var config = {
package: {
scripts: {
start: "./node_modules/.bin/browser-refresh server/index.js"
}
},
dependencies: {
"less": "^2.7",
"lasso": "^2.8.3",
"lasso-marko": "^2.1.0",
"lasso-less": "^2.1.0",
"marko": "v4.0.0-rc.3",
"browser-refresh-taglib": "*",
},
devDependencies: {
"browser-refresh": "^1.7.0",
},
server: serverConfig,
}

config.installs = Object.keys(config.dependencies)


// git ignore generated files
var gitignorePath = baseConfig.projectRoot + '/.gitignore'
var gitignore = vfs.read(gitignorePath)
vfs.write(gitignorePath, gitignore + '\n*.marko.js\n.cache/\nclient/public/static/\n')

return config
}

if ( baseConfig.package.addedPultModules.includes('spa') ) {
throw new lib.errors.ModuleError('Module `marko` is incompatible with the module `spa`')
}


var serverConfig = lib.clone(baseConfig.server)

serverConfig.routerPipeline.unshift('./marko.js')

var config = {
package: {
scripts: {
start: "./node_modules/.bin/browser-refresh server/index.js"
}
},
dependencies: {
"less": "^2.7",
"lasso": "^2.8.3",
"lasso-marko": "^2.1.0",
"lasso-less": "^2.1.0",
"marko": "v4.0.0-rc.3",
"browser-refresh-taglib": "*",
},
devDependencies: {
"browser-refresh": "^1.7.0",
},
server: serverConfig,
}

config.installs = Object.keys(config.dependencies)


// git ignore generated files
var gitignorePath = baseConfig.projectRoot + '/.gitignore'
var gitignore = vfs.read(gitignorePath)
vfs.write(gitignorePath, gitignore + '\n*.marko.js\n.cache/\nclient/public/static/\n')

return config
}
Loading

0 comments on commit 377103f

Please sign in to comment.