Skip to content

Commit

Permalink
Exit successfully if the shim is already what we want it to be. (ampp…
Browse files Browse the repository at this point in the history
…roject#6416)

* Exit successfully if the shim is already what we want it to be.

* typo
  • Loading branch information
powdercloud authored and Vanessa Pasque committed Dec 22, 2016
1 parent 1e7b9fb commit 18e05af
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
5 changes: 5 additions & 0 deletions validator/nodejs/README.md
Expand Up @@ -65,3 +65,8 @@ As expected, this emits errors because the provided string in the example, `<htm
### 1.0.16
* `npm install amphtml-validator` (local install) should now work on Windows,
for `require('amphtml-validator')`.

### 1.0.17
* If the amphtml-validator command is already patched up for Windows, leave it
alone instead of failing. Relevant if the package has been installed globally
and now we're performing a local install on top of it.
2 changes: 1 addition & 1 deletion validator/nodejs/package.json
@@ -1,6 +1,6 @@
{
"name": "amphtml-validator",
"version": "1.0.16",
"version": "1.0.17",
"description": "Official validator for AMP HTML (www.ampproject.org)",
"keywords": ["AMP", "validator", "validate", "AMP HTML", "Accelerated Mobile Pages"],
"engines": {
Expand Down
38 changes: 26 additions & 12 deletions validator/nodejs/postinstall-windows.js
Expand Up @@ -46,25 +46,39 @@ if (!fs.existsSync(validatorShimPath)) {
process.exit(0);
}

// We take a quick look into the shim file that npm for Windows generates.
// This will try to invoke the shell script, using /bin/sh as the interpreter -
// which we already established won't work if this postinstall were to trigger
// (again, see the command line in package.json and imagine what happens if
// cmd.exe executes it).
// Unfortunately the shim file that npm for Windows generates doesn't
// work. It will try to invoke the shell script that's part of this
// package, using /bin/sh as the interpreter - which we've already
// established won't work if this postinstall were to trigger (again,
// see the command line in package.json and imagine what happens if
// cmd.exe executes it). So, we detect this file and replace it with a
// shim that will likely work. We already know that the node command
// works (since this is how we were invoked from the command line in
// package.json), and we know that index.js is a sibling to
// index.sh. So, we can just invoke that.
var shimForWindows = '@ECHO OFF\r\n' +
'node "%~dp0\\node_modules\\amphtml-validator\\index.js" %*';

var contents = fs.readFileSync(validatorShimPath, 'utf8');
// This check triggers specifically if amphtml-validator has been globally
// installed already and now we're performing a local install. This crude
// postinstall script will then nevertheless reach for the global installation
// which may already be patched up. But it's a good idea to be idempotent in
// general.
if (contents === shimForWindows) {
console./*OK*/ log('postinstall-windows.js: amphtml-validator already fine.');
process.exit(0);
}

// Before we write the modified shim we still check the contents of the file
// to make sure it's not something unexpected.
if (contents.indexOf('"%~dp0\\node_modules\\amphtml-validator\\index.sh"') ===
-1) {
console./*OK*/ error(
'postinstall-windows.js: amphtml-validator not matched.');
process.exit(1);
}

// Now we write a shim file that will likely work. We already know that the
// node command works (since this is how we were invoked from the command line
// in package.json), and we know that index.js is a sibling to index.sh.
// So, we can just invoke that.
fs.writeFileSync(
validatorShimPath, '@ECHO OFF\r\n' +
'node "%~dp0\\node_modules\\amphtml-validator\\index.js" %*');
fs.writeFileSync(validatorShimPath, shimForWindows);
console./*OK*/ log(
'postinstall-windows.js: Modified amphtml-validator for Windows.');

0 comments on commit 18e05af

Please sign in to comment.