Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel authored and Angel committed Oct 20, 2017
0 parents commit d96c77b
Show file tree
Hide file tree
Showing 7 changed files with 538 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# IDEA folders
.idea/

# Chromium binaries output folder
/lib/
7 changes: 7 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const path = require('path');

module.exports = {
BIN_OUT_PATH: path.join(__dirname, 'lib', 'chromium')
};
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const config = require('./config');
const utils = require('./utils');

const fs = require('fs');

function getBinaryPath() {
const path = utils.getOsChromiumBinPath();

console.log('Chromiu: ', path);

if (fs.existsSync(path)) {
return path;
}

return undefined;
}

module.exports = {
path: getBinaryPath()
};
107 changes: 107 additions & 0 deletions install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict';

const config = require('./config');
const utils = require('./utils');

const extractZip = require('extract-zip');
const got = require('got');
const tmp = require('tmp');
const fs = require('fs');

const CDN_URL = 'https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/';


function getOsCdnUrl() {
let url = CDN_URL;

const platform = process.platform;

if (platform === 'linux') {
url += 'Linux';
if (process.arch === 'x64') {
url += '_x64';
}
} else if (platform === 'win32') {
url += 'Win';
if (process.arch === 'x64') {
url += '_x64';
}
} else if (platform === 'darwin') {
url += 'Mac';
} else {
console.log('Unknown platform or architecture found:', process.platform, process.arch);
process.exit(1);
}

return url;
}

function getLatestRevisionNumber() {
return new Promise((resolve, reject) => {
const url = getOsCdnUrl() + '%2FLAST_CHANGE?alt=media';
got(url)
.then(response => {
resolve(response.body);
})
.catch(error => {
console.log('An error occured while trying to retrieve latest revision number', error);
reject(error);
});
});
}

function createTempFile() {
return new Promise((resolve, reject) => {
tmp.file((error, path, fd) => {
if (error) {
console.log('An error occured while trying to create temporary file', error);
reject(error);
} else {
resolve(path);
}
});
});
}

function downloadChromiumRevision(revision) {
return new Promise((resolve, reject) => {
createTempFile()
.then(path => {
console.log('Downloading Chromium archive from Google CDN');
const url = getOsCdnUrl() + `%2F${revision}%2F` + utils.getOsChromiumFolderName() + '.zip?alt=media';
got.stream(url)
.on('error', (error, body, response) => {
console.log('An error occurred while trying to download Chromium archive', error);
reject(error);
})
.pipe(fs.createWriteStream(path))
.on('error', error => {
console.log('An error occurred while trying to save Chromium archive to disk', error);
reject(error);
})
.on('finish', () => {
resolve(path);
});
});
});
}

function unzipArchive(archivePath, outputFolder) {
console.log('Started extracting archive', archivePath);
return new Promise((resolve, reject) => {
extractZip(archivePath, { dir: outputFolder }, error => {
if (error) {
console.log('An error occurred while trying to extract archive', error);
reject(error);
} else {
console.log('Archive was successfully extracted');
resolve(true);
}
})
});
}

getLatestRevisionNumber()
.then(downloadChromiumRevision)
.then(path => unzipArchive(path, config.BIN_OUT_PATH))
.catch(error => console.error('An error occurred while trying to setup Chromium. Resolve all issues and restart the process', error));
Loading

0 comments on commit d96c77b

Please sign in to comment.