-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DX-185] install compiler inside each components' dir #710
Changes from all commits
6690987
2bf6a21
e564ef5
7ebdcc7
6ccdb6e
eb3e0e0
5af1975
033f792
2261d07
9a094bf
8de124b
a0f560a
00211e4
4a8465d
1f2bc22
2d5b184
a23e539
ac45083
a059b76
0747c8d
5e0279a
41d8bcd
4ac814a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const format = require('stringformat'); | ||
|
||
const strings = require('../../../resources'); | ||
|
||
module.exports = (options, cb) => { | ||
const { componentPath, pkg, template } = options; | ||
const compilerDep = `${template}-compiler`; | ||
const isOk = pkg.devDependencies[compilerDep]; | ||
|
||
const err = isOk | ||
? null | ||
: format(strings.errors.cli.TEMPLATE_DEP_MISSING, template, componentPath); | ||
|
||
cb(err, compilerDep); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
const cleanRequire = require('../../../utils/clean-require'); | ||
const installCompiler = require('./install-compiler'); | ||
|
||
module.exports = (options, cb) => { | ||
const { compilerDep, componentPath, logger, pkg } = options; | ||
const compilerPath = path.join(componentPath, 'node_modules', compilerDep); | ||
const compiler = cleanRequire(compilerPath, { justTry: true }); | ||
|
||
if (compiler) { | ||
return cb(null, compiler); | ||
} | ||
|
||
let dependency = compilerDep; | ||
if (pkg.devDependencies[compilerDep]) { | ||
dependency += `@${pkg.devDependencies[compilerDep]}`; | ||
} | ||
|
||
const installOptions = { | ||
compilerPath, | ||
componentName: pkg.name, | ||
componentPath, | ||
dependency, | ||
logger | ||
}; | ||
|
||
installCompiler(installOptions, cb); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const _ = require('lodash'); | ||
|
||
const cleanRequire = require('../../../utils/clean-require'); | ||
|
||
module.exports = dependencies => { | ||
const missing = []; | ||
_.each(dependencies, (version, dependency) => { | ||
const pathToModule = path.resolve('node_modules/', dependency); | ||
if (!cleanRequire(pathToModule, { justTry: true })) { | ||
missing.push(`${dependency}@${version || 'latest'}`); | ||
} | ||
}); | ||
return missing; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const async = require('async'); | ||
const coreModules = require('builtin-modules'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
|
||
const ensureCompilerIsDeclaredAsDevDependency = require('./ensure-compiler-is-declared-as-devDependency'); | ||
const getCompiler = require('./get-compiler'); | ||
const installMissingDependencies = require('./install-missing-dependencies'); | ||
const isTemplateLegacy = require('./is-template-legacy'); | ||
const strings = require('../../../resources'); | ||
|
||
const getComponentPackageJson = (componentPath, cb) => | ||
fs.readJson(path.join(componentPath, 'package.json'), cb); | ||
|
||
module.exports = (options, callback) => { | ||
const { components, logger } = options; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for the future: I guess we could start to use destructuring in the signature leve for cases like this. |
||
|
||
const dependencies = {}; | ||
const addDependencies = componentDependencies => | ||
_.each(componentDependencies || {}, (version, dependency) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lodash each already support passing in undefined for the collection item |
||
dependencies[dependency] = version; | ||
}); | ||
|
||
const templates = {}; | ||
const addTemplate = (templateName, template) => { | ||
templates[templateName] = template; | ||
}; | ||
|
||
const setupComponentDependencies = (componentPath, done) => | ||
async.waterfall( | ||
[ | ||
cb => getComponentPackageJson(componentPath, cb), | ||
(pkg, cb) => { | ||
addDependencies(pkg.dependencies); | ||
|
||
const template = pkg.oc.files.template.type; | ||
if (isTemplateLegacy(template)) { | ||
return done(); | ||
} | ||
|
||
cb(null, { componentPath, logger, pkg, template }); | ||
}, | ||
|
||
(options, cb) => | ||
ensureCompilerIsDeclaredAsDevDependency(options, (err, compilerDep) => | ||
cb(err, _.extend(options, { compilerDep })) | ||
), | ||
|
||
(options, cb) => | ||
getCompiler(options, (err, compiler) => | ||
cb(err, _.extend(options, { compiler })) | ||
), | ||
|
||
(options, cb) => { | ||
const { compiler, template } = options; | ||
addTemplate(template, compiler); | ||
cb(); | ||
} | ||
], | ||
done | ||
); | ||
|
||
logger.warn(strings.messages.cli.CHECKING_DEPENDENCIES); | ||
async.eachSeries(components, setupComponentDependencies, err => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
const result = { | ||
modules: _.union(coreModules, _.keys(dependencies)).sort(), | ||
templates: _.values(templates) | ||
}; | ||
|
||
const installOptions = { dependencies, logger }; | ||
installMissingDependencies(installOptions, err => callback(err, result)); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
const format = require('stringformat'); | ||
|
||
const cleanRequire = require('../../../utils/clean-require'); | ||
const isTemplateValid = require('../../../utils/is-template-valid'); | ||
const npm = require('../../../utils/npm-utils'); | ||
const strings = require('../../../resources/index'); | ||
|
||
module.exports = (options, cb) => { | ||
const { | ||
compilerPath, | ||
componentName, | ||
componentPath, | ||
dependency, | ||
logger | ||
} = options; | ||
|
||
logger.warn(format(strings.messages.cli.INSTALLING_DEPS, dependency), true); | ||
|
||
const npmOptions = { | ||
dependency, | ||
installPath: componentPath, | ||
save: false, | ||
silent: true | ||
}; | ||
|
||
npm.installDependency(npmOptions, err => { | ||
err ? logger.err('FAIL') : logger.ok('OK'); | ||
const compiler = cleanRequire(compilerPath, { justTry: true }); | ||
const isOk = isTemplateValid(compiler); | ||
const errorMsg = 'There was a problem while installing the compiler'; | ||
cb(!err && isOk ? null : errorMsg, compiler); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const format = require('stringformat'); | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
|
||
const getMissingDependencies = require('./get-missing-dependencies'); | ||
const npm = require('../../../utils/npm-utils'); | ||
const strings = require('../../../resources/index'); | ||
|
||
module.exports = (options, callback) => { | ||
const { dependencies, logger } = options; | ||
|
||
const missing = getMissingDependencies(dependencies); | ||
|
||
if (_.isEmpty(missing)) { | ||
return callback(null); | ||
} | ||
|
||
logger.warn( | ||
format(strings.messages.cli.INSTALLING_DEPS, missing.join(', ')), | ||
true | ||
); | ||
|
||
const npmOptions = { | ||
dependencies: missing, | ||
installPath: path.resolve('.'), | ||
save: false, | ||
silent: true | ||
}; | ||
|
||
npm.installDependencies(npmOptions, err => { | ||
if (err || !_.isEmpty(getMissingDependencies(dependencies))) { | ||
logger.fail('FAIL'); | ||
return callback(strings.errors.cli.DEPENDENCIES_INSTALL_FAIL); | ||
} | ||
logger.ok('OK'); | ||
callback(null); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = template => !!{ jade: true, handlebars: true }[template]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const format = require('stringformat'); | ||
const path = require('path'); | ||
|
||
const cleanRequire = require('../../../utils/clean-require'); | ||
const isTemplateLegacy = require('./is-template-legacy'); | ||
const isTemplateValid = require('../../../utils/is-template-valid'); | ||
const strings = require('../../../resources'); | ||
|
||
module.exports = function(template, options) { | ||
const requireOptions = options || {}; | ||
let ocTemplate; | ||
|
||
if (isTemplateLegacy(template)) { | ||
template = `oc-template-${template}`; | ||
} | ||
|
||
if (requireOptions.compiler) { | ||
template = `${template}-compiler`; | ||
} | ||
|
||
const localTemplate = path.join(__dirname, '../../node_modules', template); | ||
const relativeTemplate = path.resolve('.', 'node_modules', template); | ||
const componentRelativePath = path.join( | ||
requireOptions.componentPath, | ||
'node_modules', | ||
template | ||
); | ||
|
||
[ | ||
componentRelativePath, | ||
template, | ||
localTemplate, | ||
relativeTemplate | ||
].forEach(pathToTry => { | ||
ocTemplate = ocTemplate || cleanRequire(pathToTry, { justTry: true }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know about some. Perhaps let's do it in another PR as optimisation? |
||
}); | ||
|
||
if (!ocTemplate) { | ||
throw new Error(format(strings.errors.cli.TEMPLATE_NOT_FOUND, template)); | ||
} | ||
|
||
if (!isTemplateValid(ocTemplate, requireOptions)) { | ||
throw new Error( | ||
format(strings.errors.cli.TEMPLATE_TYPE_NOT_VALID, template) | ||
); | ||
} | ||
|
||
return ocTemplate; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will happen in the case of someone having something like
*
or a relative path ?