Created
December 14, 2015 07:49
-
-
Save aliaksandr-master/10ab775bb4bbc8d3652a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var glob = require('glob'); | |
var path = require('path'); | |
var _ = require('lodash'); | |
var fs = require('fs'); | |
var chalk = require('chalk'); | |
glob.sync(path.resolve(__dirname, 'app/components/**/*.js')).forEach(function (f) { | |
var content = fs.readFileSync(f, { encoding: 'utf8' }); | |
var USESTRIC_EXP = /^'use strict';/; | |
if (!USESTRIC_EXP.test(content)) { | |
throw new Error(chalk.red('!!!!undefined useStrict')); | |
} | |
if (/^'use strict';\s+require\(/.test(content)) { | |
return; | |
} | |
var filename = path.basename(f, '.js'); | |
var dirPath = path.dirname(f); | |
var dirname = path.basename(dirPath); | |
if (!/^(?:b-.+|index)$/.test(filename)) { | |
return; | |
} | |
var base = filename; | |
if (filename === 'index') { | |
base = 'b-' + dirname.replace('.', '-'); | |
} | |
var lessFile = base + '.less'; | |
var htmlFile = base + '.html'; | |
var src = content; | |
var addedHtml = false; | |
var addedLess = false; | |
content = content.replace(USESTRIC_EXP, function ($0) { | |
var prev = $0; | |
$0 = $0 + '\n'; | |
if (fs.existsSync(dirPath + '/' + lessFile)) { | |
$0 = $0 + '\nrequire(\'./' + lessFile + '\');\n'; | |
addedLess = true; | |
} else { | |
console.log('!!!!', lessFile); | |
} | |
if (fs.existsSync(dirPath + '/' + htmlFile)) { | |
$0 = $0 + '\nvar template = require(\'./' + htmlFile + '\');\n'; | |
addedHtml = true; | |
} else { | |
console.log('!!!!', htmlFile); | |
} | |
$0 = $0 + '\n'; | |
return addedHtml || addedLess ? $0 : prev; | |
}); | |
if (addedHtml) { | |
content = content.replace(/templateUrl: '(?:[^']+)'(,)?/, 'template: template$1'); | |
} | |
if (/templateUrl:/.test(content)) { | |
console.log('@@@@', 'ERROR', 'exists "templateUrl:" IN', dirname + '/' + filename, addedHtml); | |
return; | |
} | |
if (src.length === content.length) { | |
return; | |
} | |
fs.writeFileSync(f, content, { encoding: 'utf8' }); | |
console.log(dirname, '/', filename, lessFile, htmlFile, addedHtml, addedLess); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment