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

Fix online test in prepare task #44

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"repository": "frontend/toolbox-utils",
"author": {
"url": "https://github.com/frontend"
"url": "https://github.com/frontend",
"name": "Frontend Team"
},
"babel": {
"presets": [
Expand Down Expand Up @@ -80,7 +81,6 @@
"jquery": "^3.4.1",
"latest-version": "^5.1.0",
"merge-stream": "^1.0.1",
"node-fetch": "^2.5.0",
"postcss-reporter": "^6.0.1",
"postcss-scss": "^2.0.0",
"stylelint": "^10.0.1",
Expand Down
56 changes: 28 additions & 28 deletions tasks/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@ const $ = require('gulp-load-plugins')();
const config = require('./config');
const fs = require('fs-extra');
const dirTree = require('./helpers').dirTree;
const fetch = require('node-fetch');
const yaml = require('yamljs');
const pkg = require('./../package.json');

const cssBundles = config.bundles !== undefined && config.bundles.scss !== undefined;
const jsBundles = config.bundles !== undefined && config.bundles.js !== undefined;

const prepare = async (done) => {
// Check if user is online
const isOnline = await axios
.get('https://google.com', { timeout: 3000 })
.then(res => true)
.catch(err => false);
let cdn;

// Get CDN from jsdeliver.
cdn = await axios
.get('https://cdn.jsdelivr.net/gh/frontend/toolbox-reader/package.json')
.then(res => {
const minor = res.data.version.split('.').splice(0, 2).join('.');
return `https://cdn.jsdelivr.net/gh/frontend/toolbox-reader@${minor}/build/static`;
})
.catch((err) => {
if (err.response) {
log.error(`⚠️ Got "${err.response.status} ${err.response.statusText}" status while fetching toolbox-reader online.`);
} else {
log.error(`⚠️ Could not fetch toolbox-reader online. Will try to use the offline mode.`);
}
});

// Define cdn path
const localCdn = `${homedir()}/.toolbox`;
let cdn;
if (config.reader_path) {
cdn = config.reader_path;
} else {
if (isOnline) {
cdn = await axios
.get('https://cdn.jsdelivr.net/gh/frontend/toolbox-reader/package.json')
.then(res => {
const minor = res.data.version.split('.').splice(0, 2).join('.');
return `https://cdn.jsdelivr.net/gh/frontend/toolbox-reader@${minor}/build/static`;
})
.catch(err => log.error(err));

if (cdn) {
// Download Toolbox Reader bundles for future offline usage
download(`${cdn}/css/main.css`, `${localCdn}/`);
download(`${cdn}/js/main.js`, `${localCdn}/`);
} else {
// Retrieve local Toolbox Reader bundles
cdn = 'toolbox';
fs.pathExists(`${localCdn}/main.css`, (err, exists) => {
if (err || !exists) log.error('You don\'t have any local Toolbox Reader bundles to use... Please connecte yourlsef before retrying.')
const copyToDir = `${config.project}/${config.dest}toolbox`;
fs.ensureDirSync(copyToDir)
fs.copy(`${localCdn}/main.css`, `${copyToDir}/main.css`);
fs.copy(`${localCdn}/main.js`, `${copyToDir}/main.js`);
})
}

fs.pathExists(`${localCdn}/main.css`, (err, exists) => {
if (err || !exists) log.error('Unable to find a Toolbox Reader bundle locally. Make sure you are online and try again.');
const copyToDir = `${config.project}/${config.dest}toolbox`;
fs.ensureDirSync(copyToDir);
fs.copy(`${localCdn}/main.css`, `${copyToDir}/css/main.css`);
fs.copy(`${localCdn}/main.js`, `${copyToDir}/js/main.js`);
});
}

// Get local colors and data
Expand Down Expand Up @@ -113,7 +113,7 @@ const prepare = async (done) => {
window.builder = "${pkg.version}";
${ config.theme ? `window.theme = ${JSON.stringify(config.theme)};` : '' }
</script>
<link rel="stylesheet" href="${cdn}${isOnline || config.reader_path ? '/css' : ''}/main.css">
<link rel="stylesheet" href="toolbox/css/main.css">
${config.vendors.css ? '<link rel="stylesheet" href="css/vendors.min.css">' : ''}
${ cssBundles
? config.bundles.scss
Expand Down Expand Up @@ -142,10 +142,10 @@ const prepare = async (done) => {
`).appendTo('body');
}

$(` <script src="${cdn}${isOnline || config.reader_path ? '/js' : ''}/main.js"></script>\n`).appendTo('body');
$(' <script src="toolbox/js/main.js"></script>\n').appendTo('body');
}))
.pipe($.rename('index.html'))
.pipe(gulp.dest(config.dest, {cwd: config.project}));
}
};

module.exports = prepare;
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6317,10 +6317,6 @@ nice-try@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.4.tgz#d93962f6c52f2c1558c0fbda6d512819f1efe1c4"

node-fetch@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.5.0.tgz#8028c49fc1191bba56a07adc6e2a954644a48501"

[email protected]:
version "0.7.5"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.7.5.tgz#6c152c345ce11c52f465c2abd957e8639cd674df"
Expand Down