-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
chore: add script to automatically update plunker bundle #913
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
be2f15f
chore: add script to automatically update plunker bundle
devversion bdcd2ea
ci: add plunker mode to tracis
devversion 0caa375
Build project through npm scripts, to avoid path issues.
devversion eeb2225
Enable travis mode
devversion a32f03a
address style changes
devversion e259130
Do not run Plunker creation on Pull Requests
devversion f9cb322
Use new credentials for plnkr firebase
devversion 5256737
Do not fallback to build, if it the current build is a pull request
devversion e7f3906
Check for Plunker Token instead
devversion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
/* | ||
* This script creates a bundle of all components and publishes it to Firebase. | ||
* The bundle will be used to load a Plunker Demo of Angular Material 2. | ||
*/ | ||
|
||
const globSync = require('glob').sync; | ||
const spawnSync = require('child_process').spawnSync; | ||
const execSync = require('child_process').execSync; | ||
const firebase = require('firebase-tools'); | ||
const path = require('path'); | ||
const fse = require('fs-extra'); | ||
const inlineResources = require('../../tools/inline-resources-tools'); | ||
|
||
const ROOT = path.join(__dirname, '..', '..'); | ||
const DIST_ROOT = path.join(ROOT, 'dist'); | ||
const DEPLOY_ROOT = path.join(DIST_ROOT, 'plunker-deploy/'); | ||
|
||
const mainFile = path.join(DIST_ROOT, 'main.js'); | ||
const latestTag = getLatestTag(); | ||
const isRelease = getShaFromTag(latestTag) === getLatestSha(); | ||
const baseName = isRelease ? latestTag : 'HEAD'; | ||
|
||
// Remove the distribution folder. | ||
fse.removeSync(DIST_ROOT); | ||
|
||
if (!buildProject()) { | ||
console.error('An error occurred while building the project.'); | ||
process.exit(1); | ||
} | ||
|
||
// Inline the resources into the bundle file. | ||
inlineBundle(); | ||
|
||
// Create distribution folder. | ||
fse.mkdirp(DEPLOY_ROOT); | ||
|
||
// Copy the bundle to the deploy folder. | ||
fse.copySync(mainFile, path.join(DEPLOY_ROOT, `${baseName}_bundle.js`)); | ||
|
||
firebase.deploy({ | ||
firebase: 'material2-plnkr', | ||
token: process.env.MATERIAL2_PLNKR_TOKEN, | ||
public: 'dist/plunker-deploy' | ||
}).then(() => { | ||
console.log('Firebase: Successfully deployed bundle to firebase.'); | ||
process.exit(0); | ||
}).catch(err => { | ||
console.error('Firebase: An error occurred while deploying to firebase.'); | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
|
||
function inlineBundle() { | ||
let filePathFn = (sourceFile) => { | ||
let sourceFiles = globSync(`**/${sourceFile}`, { cwd: DIST_ROOT }); | ||
return path.resolve(DIST_ROOT, sourceFiles[0]); | ||
}; | ||
|
||
executeInline(inlineResources.inlineStyle); | ||
executeInline(inlineResources.inlineTemplate); | ||
|
||
function executeInline(inlineFn) { | ||
fse.writeFileSync(mainFile, inlineFn(filePathFn, fse.readFileSync(mainFile).toString())); | ||
} | ||
} | ||
|
||
function buildProject() { | ||
// Note: We can't use spawnSync here, because on some environments the Angular CLI | ||
// is not added to the System Paths and is only available in the locals. | ||
let out = execSync('npm run build:production').toString(); | ||
|
||
return out.indexOf('successfully') !== -1; | ||
} | ||
|
||
function getLatestTag() { | ||
let tagSHA = spawnSync('git', ['rev-list', '--tags', '--max-count=1']).stdout.toString().trim(); | ||
return spawnSync('git', ['describe', '--tags', tagSHA]).stdout.toString().trim(); | ||
} | ||
|
||
function getLatestSha() { | ||
return spawnSync('git', ['rev-parse', 'master']) | ||
} | ||
|
||
function getShaFromTag(tag) { | ||
return spawnSync('git', ['rev-list', '-1', tag]).stdout.toString().trim(); | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
|
||
/** | ||
* Inline the templates for a source file. Simply search for instances of `templateUrl: ...` and | ||
* replace with `template: ...` (with the content of the file included). | ||
* @param filePath {string} The path of the source file. | ||
* @param content {string} The source file's content. | ||
* @return {string} The content with all templates inlined. | ||
*/ | ||
function inlineTemplate(filePath, content) { | ||
|
||
// Transform the filePath into a function, to be able to customize the path. | ||
let fileRootFn = typeof filePath !== 'function' ? () => filePath : filePath; | ||
|
||
return content.replace(/templateUrl:\s*(?:'|")(.+?\.html)(?:"|')/g, function(m, templateUrl) { | ||
const templateFile = path.join(path.dirname(fileRootFn(templateUrl)), templateUrl); | ||
|
||
if (!fs.existsSync(templateFile)) { | ||
return; | ||
} | ||
|
||
const templateContent = fs.readFileSync(templateFile, 'utf-8'); | ||
const shortenedTemplate = templateContent | ||
.replace(/([\n\r]\s*)+/gm, ' ') | ||
.replace(/"/g, '\\"'); | ||
|
||
return `template: "${shortenedTemplate}"`; | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* Inline the styles for a source file. Simply search for instances of `styleUrls: [...]` and | ||
* replace with `styles: [...]` (with the content of the file included). | ||
* @param filePath {string} The path of the source file. | ||
* @param content {string} The source file's content. | ||
* @return {string} The content with all styles inlined. | ||
*/ | ||
function inlineStyle(filePath, content) { | ||
|
||
// Transform the filePath into a function, to be able to customize the path. | ||
let fileRootFn = typeof filePath !== 'function' ? () => filePath : filePath; | ||
|
||
return content.replace(/styleUrls:\s*(\[[\s\S]*?])/gm, function(m, styleUrls) { | ||
const urls = eval(styleUrls); | ||
|
||
let inlineStyles = urls | ||
.map(styleUrl => path.join(path.dirname(fileRootFn(styleUrl)), styleUrl)) | ||
.filter(styleUrl => fs.existsSync(styleUrl)) | ||
.map(styleFile => { | ||
const styleContent = fs.readFileSync(styleFile, 'utf-8'); | ||
const shortenedStyle = styleContent | ||
.replace(/([\n\r]\s*)+/gm, ' ') | ||
.replace(/"/g, '\\"'); | ||
|
||
return `"${shortenedStyle}"`; | ||
}); | ||
|
||
return 'styles: [' + inlineStyles.join(',\n') + ']'; | ||
}); | ||
} | ||
|
||
/** | ||
* Removes the module ids of the component metadata. | ||
* Since the templates and styles are now inlined, the module id has become unnecessary and | ||
* can cause unexpected issues. | ||
*/ | ||
function removeModuleIds(content) { | ||
// Match the line feeds as well, because we want to get rid of that line. | ||
return content.replace(/^\W+moduleId:\W+module\.id,?[\n|\r]+/gm, ''); | ||
} | ||
|
||
module.exports = { | ||
inlineStyle: inlineStyle, | ||
inlineTemplate: inlineTemplate, | ||
removeModuleIds: removeModuleIds | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wish I had known this option existed long ago...