Skip to content

Commit

Permalink
semi-colons, fixes #142 (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Bengtsson committed Feb 14, 2018
1 parent e6ad203 commit 2b0eceb
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 238 deletions.
1 change: 0 additions & 1 deletion .prettierrc
@@ -1,2 +1 @@
semi: false
singleQuote: true
72 changes: 36 additions & 36 deletions bin/e2e.js
@@ -1,73 +1,73 @@
#!/usr/bin/env node
const fs = require('fs')
const spawn = require('child_process').spawnSync
const fs = require('fs');
const spawn = require('child_process').spawnSync;

const inCI = process.env.CI || false
const inCI = process.env.CI || false;

function assert(truth, failure) {
if (!truth) {
console.error(failure)
process.exit(1)
console.error(failure);
process.exit(1);
}
}
function assertStatus(spawned) {
if (spawned.status !== 0) {
console.error('STDOUT', spawned.stdout.toString())
console.error('STDERR', spawned.stderr.toString())
process.exit(spawned.status)
console.error('STDOUT', spawned.stdout.toString());
console.error('STDERR', spawned.stderr.toString());
process.exit(spawned.status);
}
}

const version = spawn('./bin/minimalcss.js', ['--version'])
assertStatus(version)
const versionNumber = version.stdout.toString()
assert(!/[^\d\.]/.test(versionNumber.trim()), 'Not a version number')
const versionNumber2 = spawn('./bin/minimalcss.js', ['-v']).stdout.toString()
assert(versionNumber2 === versionNumber, 'alias not working')
const version = spawn('./bin/minimalcss.js', ['--version']);
assertStatus(version);
const versionNumber = version.stdout.toString();
assert(!/[^\d\.]/.test(versionNumber.trim()), 'Not a version number');
const versionNumber2 = spawn('./bin/minimalcss.js', ['-v']).stdout.toString();
assert(versionNumber2 === versionNumber, 'alias not working');

function openUrl(url, ...options) {
console.log(`Opening ${url} ...`)
console.log(`Opening ${url} ...`);
if (inCI) {
options.push('--nosandbox')
options.push('--nosandbox');
}
options.push(url)
const t0 = new Date()
const opened = spawn('./bin/minimalcss.js', options)
assertStatus(opened)
const t1 = new Date()
const t = (t1 - t0) / 1000
console.log(`Took ${t.toFixed(2)}s`)
return Promise.resolve(opened)
options.push(url);
const t0 = new Date();
const opened = spawn('./bin/minimalcss.js', options);
assertStatus(opened);
const t1 = new Date();
const t = (t1 - t0) / 1000;
console.log(`Took ${t.toFixed(2)}s`);
return Promise.resolve(opened);
}

// Simplest form of opening
openUrl('https://en.wikipedia.org/wiki/List_of_HTTP_status_codes').then(
spawned => {
const css = spawned.stdout.toString()
const css = spawned.stdout.toString();
assert(
css.length > 20000 && css.length < 30000,
'Expect CSS to be between 20,000...30,000'
)
);
}
)
);

// Open with -o
openUrl('https://nodejs.org/', '-o', '/tmp/nodejs.css').then(spawned => {
const stdout = spawned.stdout.toString()
assert(!stdout.trim(), 'Output should be empty')
const css = fs.readFileSync('/tmp/nodejs.css').toString()
const stdout = spawned.stdout.toString();
assert(!stdout.trim(), 'Output should be empty');
const css = fs.readFileSync('/tmp/nodejs.css').toString();
assert(
css.length > 1000 && css.length < 15000,
'Expect CSS to be between 1K...15K'
)
})
);
});

// With verbose output
openUrl('https://news.ycombinator.com/', '--verbose').then(spawned => {
const css = spawned.stdout.toString()
assert(/\/\*\nGenerated /.test(css), 'Expected verbose leading comment')
const css = spawned.stdout.toString();
assert(/\/\*\nGenerated /.test(css), 'Expected verbose leading comment');
assert(
css.length > 1000 && css.length < 3000,
'Expect CSS to be between 1K...3K'
)
})
);
});
108 changes: 54 additions & 54 deletions bin/minimalcss.js
@@ -1,14 +1,14 @@
#!/usr/bin/env node

'use strict'
'use strict';

const { URL } = require('url')
const fs = require('fs')
const minimist = require('minimist')
const minimalcss = eval('require')('../index')
const filesize = require('filesize')
const { URL } = require('url');
const fs = require('fs');
const minimist = require('minimist');
const minimalcss = eval('require')('../index');
const filesize = require('filesize');

const args = process.argv.slice(2)
const args = process.argv.slice(2);

const argv = minimist(args, {
boolean: [
Expand All @@ -32,15 +32,15 @@ const argv = minimist(args, {
},
unknown: param => {
if (param.startsWith('-')) {
console.warn('Ignored unknown option: ' + param + '\n')
return false
console.warn('Ignored unknown option: ' + param + '\n');
return false;
}
}
})
});

if (argv['version']) {
console.log(minimalcss.version)
process.exit(0)
console.log(minimalcss.version);
process.exit(0);
}

if (argv['help']) {
Expand All @@ -58,93 +58,93 @@ if (argv['help']) {
" --nosandbox Adds `['--no-sandbox', '--disable-setuid-sandbox']` to puppeteer launch.\n" +
' --version or -v Print minimalcss version.\n' +
''
)
process.exit(0)
);
process.exit(0);
}

const urls = argv['_']
const urls = argv['_'];

urls.forEach(url => {
try {
const parsed = new URL(url)
const parsed = new URL(url);
} catch (ex) {
console.error(`${url} is not a valid URL`)
process.exit(1)
console.error(`${url} is not a valid URL`);
process.exit(1);
}
})
});

const parseViewport = asString => {
if (!asString) {
return null
return null;
}
try {
return JSON.parse(asString)
return JSON.parse(asString);
} catch (ex) {
console.error(`Unable to parse 'viewport' (${ex.toString()})`)
process.exit(2)
console.error(`Unable to parse 'viewport' (${ex.toString()})`);
process.exit(2);
}
}
};

const options = {
urls: urls,
debug: argv['debug'],
loadimages: argv['loadimages'],
withoutjavascript: argv['withoutjavascript'],
skippable: request => {
let skips = argv['skip']
let skips = argv['skip'];
if (!skips) {
return false
return false;
}
if (!Array.isArray(skips)) {
skips = [skips]
skips = [skips];
}
return skips.some(skip => !!request.url().match(skip))
return skips.some(skip => !!request.url().match(skip));
},
viewport: parseViewport(argv['viewport']),
puppeteerArgs: argv['nosandbox']
? ['--no-sandbox', '--disable-setuid-sandbox']
: []
}
};

const start = Date.now()
const start = Date.now();

minimalcss
.minimize(options)
.then(result => {
let output = result.finalCss
const end = Date.now()
let output = result.finalCss;
const end = Date.now();
if (argv['verbose']) {
const now = new Date().toISOString()
let comment = `/*\nGenerated ${now} by minimalcss.\n`
const seconds = ((end - start) / 1000).toFixed(2)
const bytesHuman = filesize(output.length)
const stylesheetContents = result.stylesheetContents
const stylesheets = Object.keys(stylesheetContents)
const now = new Date().toISOString();
let comment = `/*\nGenerated ${now} by minimalcss.\n`;
const seconds = ((end - start) / 1000).toFixed(2);
const bytesHuman = filesize(output.length);
const stylesheetContents = result.stylesheetContents;
const stylesheets = Object.keys(stylesheetContents);
const totalSizeBefore = stylesheets.reduce(
(acc, key) => acc + stylesheetContents[key].length,
0
)
const totalSizeBeforeHuman = filesize(totalSizeBefore)
comment += `Took ${seconds} seconds to generate ${bytesHuman} of CSS.\n`
comment += `Based on ${stylesheets.length} stylesheets `
comment += `totalling ${totalSizeBeforeHuman}.\n`
comment += 'Options: ' + JSON.stringify(options, undefined, 2) + '\n'
comment += '*/'
output = `${comment}\n${output}`
);
const totalSizeBeforeHuman = filesize(totalSizeBefore);
comment += `Took ${seconds} seconds to generate ${bytesHuman} of CSS.\n`;
comment += `Based on ${stylesheets.length} stylesheets `;
comment += `totalling ${totalSizeBeforeHuman}.\n`;
comment += 'Options: ' + JSON.stringify(options, undefined, 2) + '\n';
comment += '*/';
output = `${comment}\n${output}`;
}
if (argv['output']) {
const filename = argv['output']
const filename = argv['output'];
try {
fs.writeFileSync(filename, output + '\n', 'utf8')
fs.writeFileSync(filename, output + '\n', 'utf8');
} catch (err) {
console.error('Unable to write file: ' + filename + '\n' + err)
process.exit(2)
console.error('Unable to write file: ' + filename + '\n' + err);
process.exit(2);
}
} else {
console.log(output)
console.log(output);
}
})
.catch(error => {
console.error(error)
process.exit(3)
})
console.error(error);
process.exit(3);
});

0 comments on commit 2b0eceb

Please sign in to comment.