Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from Crazyht/dev
Browse files Browse the repository at this point in the history
Fix eslint error detected by codacy
  • Loading branch information
Crazyht authored Jul 13, 2017
2 parents e9899e9 + 9c13e12 commit a6b2a59
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 126 deletions.
94 changes: 48 additions & 46 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,49 @@ const tempLibFolder = path.join(compilationFolder, 'lib');
const es5OutputFolder = path.join(compilationFolder, 'lib-es5');
const es2015OutputFolder = path.join(compilationFolder, 'lib-es2015');



// Recursively create a dir.
function _recursiveMkDir(dir) {
if (!fs.existsSync(dir)) {
_recursiveMkDir(path.dirname(dir));
fs.mkdirSync(dir);
}
}

// Copy files maintaining relative paths.
function _relativeCopy(fileGlob, from, to) {
return new Promise((resolve, reject) => {
glob(fileGlob, {
cwd: from,
nodir: true
}, (err, files) => {
if (err) { reject(err); }
files.forEach((file) => {
const origin = path.join(from, file);
const dest = path.join(to, file);
const data = fs.readFileSync(origin, 'utf-8');
_recursiveMkDir(path.dirname(dest));
fs.writeFileSync(dest, data);
resolve();
});
});
});
}

function compileSassFiles() {
return execa('node-sass', [
tempLibFolder,
'-o', tempLibFolder,
'--output-style',
'compressed',
'--source-map',
true,
'--source-map-contents'
]);
}


return Promise.resolve()
// Copy library to temporary folder, compile sass files and inline html/css.
.then(() => _relativeCopy(`**/*`, srcFolder, tempLibFolder)
Expand All @@ -34,14 +77,14 @@ return Promise.resolve()
.then(() => ngc({
project: `${tempLibFolder}/tsconfig.lib.json`
})
.then(exitCode => exitCode === 0 ? Promise.resolve() : Promise.reject())
.then( (exitCode) => exitCode === 0 ? Promise.resolve() : Promise.reject() )
.then(() => console.log('ES2015 compilation succeeded.'))
)
// Compile to ES5.
.then(() => ngc({
project: `${tempLibFolder}/tsconfig.es5.json`
})
.then(exitCode => exitCode === 0 ? Promise.resolve() : Promise.reject())
.then( (exitCode) => exitCode === 0 ? Promise.resolve() : Promise.reject() )
.then(() => console.log('ES5 compilation succeeded.'))
)
// Copy typings and metadata to `dist/` folder.
Expand Down Expand Up @@ -122,10 +165,10 @@ return Promise.resolve()
minifiedUmdConfig,
fesm5config,
fesm2015config
].map(cfg => rollup.rollup(cfg).then(bundle => bundle.write(cfg)));
].map((cfg) => rollup.rollup(cfg).then((bundle) => bundle.write(cfg)));

return Promise.all(allBundles)
.then(() => console.log('All bundles generated successfully.'))
.then(() => console.log('All bundles generated successfully.'));
})
// Copy package files
.then(() => Promise.resolve()
Expand All @@ -134,49 +177,8 @@ return Promise.resolve()
.then(() => _relativeCopy('README.md', rootFolder, distFolder))
.then(() => console.log('Package files copy succeeded.'))
)
.catch(e => {
.catch((e) => {
console.error('\Build failed. See below for errors.\n');
console.error(e);
process.exit(1);
});


// Copy files maintaining relative paths.
function _relativeCopy(fileGlob, from, to) {
return new Promise((resolve, reject) => {
glob(fileGlob, {
cwd: from,
nodir: true
}, (err, files) => {
if (err) reject(err);
files.forEach(file => {
const origin = path.join(from, file);
const dest = path.join(to, file);
const data = fs.readFileSync(origin, 'utf-8');
_recursiveMkDir(path.dirname(dest));
fs.writeFileSync(dest, data);
resolve();
})
})
});
}

// Recursively create a dir.
function _recursiveMkDir(dir) {
if (!fs.existsSync(dir)) {
_recursiveMkDir(path.dirname(dir));
fs.mkdirSync(dir);
}
}

function compileSassFiles() {
return execa('node-sass', [
tempLibFolder,
'-o', tempLibFolder,
'--output-style',
'compressed',
'--source-map',
true,
'--source-map-contents'
]);
}
79 changes: 39 additions & 40 deletions inline-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,6 @@ function promiseify(fn) {
const readFile = promiseify(fs.readFile);
const writeFile = promiseify(fs.writeFile);

/**
* Inline resources in a tsc/ngc compilation.
* @param projectPath {string} Path to the project.
*/
function inlineResources(projectPath) {

// Match only TypeScript files in projectPath.
const files = glob.sync('**/*.ts', {cwd: projectPath});

// For each file, inline the templates and styles under it and write the new file.
return Promise.all(files.map(filePath => {
const fullFilePath = path.join(projectPath, filePath);
return readFile(fullFilePath, 'utf-8')
.then(content => inlineResourcesFromString(content, url => {
// Resolve the template url.
return path.join(path.dirname(fullFilePath), url);
}))
.then(content => writeFile(fullFilePath, content))
.catch(err => {
console.error('An error occured: ', err);
});
}));
}

/**
* Inline resources from a string content.
* @param content {string} The source file's content.
* @param urlResolver {Function} A resolver that takes a URL and return a path.
* @returns {string} The content with resources inlined.
*/
function inlineResourcesFromString(content, urlResolver) {
// Curry through the inlining functions.
return [
inlineTemplate,
inlineStyle
].reduce((content, fn) => fn(content, urlResolver), content);
}

/**
* Inline the templates for a source file. Simply search for instances of `templateUrl: ...` and
* replace with `template: ...` (with the content of the file included).
Expand All @@ -84,7 +46,6 @@ function inlineTemplate(content, urlResolver) {
});
}


/**
* Inline the styles for a source file. Simply search for instances of `styleUrls: [...]` and
* replace with `styles: [...]` (with the content of the file included).
Expand All @@ -96,7 +57,7 @@ function inlineStyle(content, urlResolver) {
return content.replace(/styleUrls:\s*(\[[\s\S]*?\])/gm, function (m, styleUrls) {
const urls = eval(styleUrls);
return 'styles: ['
+ urls.map(styleUrl => {
+ urls.map( (styleUrl) => {
const styleFile = urlResolver(styleUrl);
const styleContent = fs.readFileSync(styleFile, 'utf-8');
const shortenedStyle = styleContent
Expand All @@ -109,6 +70,44 @@ function inlineStyle(content, urlResolver) {
});
}

/**
* Inline resources from a string content.
* @param content {string} The source file's content.
* @param urlResolver {Function} A resolver that takes a URL and return a path.
* @returns {string} The content with resources inlined.
*/
function inlineResourcesFromString(content, urlResolver) {
// Curry through the inlining functions.
return [
inlineTemplate,
inlineStyle
].reduce((content, fn) => fn(content, urlResolver), content);
}

/**
* Inline resources in a tsc/ngc compilation.
* @param projectPath {string} Path to the project.
*/
function inlineResources(projectPath) {

// Match only TypeScript files in projectPath.
const files = glob.sync('**/*.ts', {cwd: projectPath});

// For each file, inline the templates and styles under it and write the new file.
return Promise.all(files.map( (filePath) => {
const fullFilePath = path.join(projectPath, filePath);
return readFile(fullFilePath, 'utf-8')
.then((content) => inlineResourcesFromString(content, (url) => {
// Resolve the template url.
return path.join(path.dirname(fullFilePath), url);
}))
.then( (content) => writeFile(fullFilePath, content))
.catch((err) => {
console.error('An error occured: ', err);
});
}));
}

module.exports = inlineResources;
module.exports.inlineResourcesFromString = inlineResourcesFromString;

Expand Down
50 changes: 24 additions & 26 deletions integration/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const rollupConfig = {
entry: `${srcDir}/main-aot.js`,
sourceMap: false,
format: 'iife',
onwarn: function (warning) {
onwarn: (warning) => {
// Skip certain warnings
if (warning.code === 'THIS_IS_UNDEFINED') { return; }
// console.warn everything else
Expand All @@ -30,6 +30,27 @@ const rollupConfig = {
]
};

// Recursively create a dir.
function _recursiveMkDir(dir) {
if (!fs.existsSync(dir)) {
_recursiveMkDir(path.dirname(dir));
fs.mkdirSync(dir);
}
}

// Copy files maintaining relative paths.
function _relativeCopy(fileGlob, from, to) {
return glob(fileGlob, { cwd: from, nodir: true }, (err, files) => {
if (err) { throw err; }
files.forEach((file) => {
const origin = path.join(from, file);
const dest = path.join(to, file);
_recursiveMkDir(path.dirname(dest));
fs.createReadStream(origin).pipe(fs.createWriteStream(dest));
});
});
}

return Promise.resolve()
// Compile using ngc.
.then(() => ngc({ project: `./tsconfig.aot.json` }))
Expand All @@ -47,12 +68,12 @@ return Promise.resolve()
'styles.css'
];

return Promise.all(assets.map(asset => _relativeCopy(asset, srcDir, distDir)));
return Promise.all(assets.map( (asset) => _relativeCopy(asset, srcDir, distDir)));
})
// Bundle app.
.then(() => rollup.rollup(rollupConfig))
// Concatenate app and scripts.
.then(bundle => {
.then((bundle) => {
const appBundle = bundle.generate(rollupConfig);

const scripts = [
Expand All @@ -68,26 +89,3 @@ return Promise.resolve()

fs.writeFileSync(path.join(distDir, 'bundle.js'), concatenatedScripts);
});



// Copy files maintaining relative paths.
function _relativeCopy(fileGlob, from, to) {
return glob(fileGlob, { cwd: from, nodir: true }, (err, files) => {
if (err) throw err;
files.forEach(file => {
const origin = path.join(from, file);
const dest = path.join(to, file);
_recursiveMkDir(path.dirname(dest));
fs.createReadStream(origin).pipe(fs.createWriteStream(dest));
})
})
}

// Recursively create a dir.
function _recursiveMkDir(dir) {
if (!fs.existsSync(dir)) {
_recursiveMkDir(path.dirname(dir));
fs.mkdirSync(dir);
}
}
2 changes: 1 addition & 1 deletion integration/src/systemjs-angular-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;

module.exports.translate = function (load) {
if (load.source.indexOf('moduleId') != -1) return load;
if (load.source.indexOf('moduleId') !== -1) { return load; }

var url = document.createElement('a');
url.href = load.address;
Expand Down
12 changes: 6 additions & 6 deletions karma-test-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var builtPaths = (__karma__.config.builtPaths || ['src/'])
__karma__.loaded = function () { };

function isJsFile(path) {
return path.slice(-3) == '.js';
return path.slice(-3) === '.js';
}

function isSpecFile(path) {
Expand Down Expand Up @@ -78,22 +78,19 @@ System.config({
}
});

initTestBed().then(initTesting);

function initTestBed(){
return Promise.all([
System.import('@angular/core/testing'),
System.import('@angular/platform-browser-dynamic/testing')
])

.then(function (providers) {
var coreTesting = providers[0];
var browserTesting = providers[1];

coreTesting.TestBed.initTestEnvironment(
browserTesting.BrowserDynamicTestingModule,
browserTesting.platformBrowserDynamicTesting());
})
});
}

// Import all spec files and start karma
Expand All @@ -106,4 +103,7 @@ function initTesting () {
.then(__karma__.start, __karma__.error);
}

beforeEach((done) => done());
initTestBed().then(initTesting);


beforeEach((done) => done());
4 changes: 2 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ module.exports = function (config) {
browserDisconnectTolerance: 1,
browserNoActivityTimeout: 60000, //by default 10000
singleRun: false
})
}
});
};
2 changes: 1 addition & 1 deletion src/demo/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NgxTreeSelectModule } from 'ngx-tree-select';
import { Ng2BootstrapModule } from 'ngx-bootstrap';

import { AppComponent } from './app.component';
import { FlatComponent } from "./components/flat.component";
import { FlatComponent } from './components/flat.component';
import { HierarchicalComponent } from './components/hierarchical.component';
import { AppRoutes } from './app.routes';

Expand Down
2 changes: 1 addition & 1 deletion src/demo/systemjs-angular-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;

module.exports.translate = function (load) {
if (load.source.indexOf('moduleId') != -1) return load;
if (load.source.indexOf('moduleId') !== -1) { return load; }

var url = document.createElement('a');
url.href = load.address;
Expand Down
Loading

0 comments on commit a6b2a59

Please sign in to comment.