Skip to content
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

feat: [EXT-2678] add validation for build folder #475

Merged
merged 2 commits into from Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,6 @@
const ora = require('ora');
const { createZipFileFromDirectory } = require('./create-zip-from-directory');
const { validateBundle } = require('./validate-bundle');
const { showCreationError } = require('../utils');
const { createClient } = require('contentful-management');

Expand All @@ -10,6 +11,7 @@ async function createAppBundleFromFile(orgId, token, zip) {
}

async function createAppUpload(settings) {
validateBundle(settings.bundleDirectory || '.');
let appUpload = null;
const zipFileSpinner = ora('Preparing your files for upload...').start();
const zipFile = await createZipFileFromDirectory(settings.bundleDirectory || '.');
Expand Down
Expand Up @@ -31,6 +31,7 @@ describe('createAppUpload', () => {
},
},
'./create-zip-from-directory': { createZipFileFromDirectory: () => 'zip_file' },
'./validate-bundle': { validateBundle: () => {} },
}));
});

Expand Down
37 changes: 37 additions & 0 deletions packages/contentful--app-scripts/lib/upload/validate-bundle.js
@@ -0,0 +1,37 @@
const Path = require('path');
const fs = require('fs');
const chalk = require('chalk');

const ACCEPTED_ENTRY_FILES = ['index.html'];
const getEntryFile = files => files.find(file => ACCEPTED_ENTRY_FILES.includes(file));

const ABSOLUTE_PATH_REG_EXP = /(src|href)="\/([^/])([^"]*)+"/g;

const fileContainsAbsolutePath = fileContent => {
return [...fileContent.matchAll(ABSOLUTE_PATH_REG_EXP)].length > 0;
};

const validateBundle = path => {
const buildFolder = Path.join('./', path);
const files = fs.readdirSync(buildFolder);
const entry = getEntryFile(files);
if (!entry) {
throw new Error('Make sure your bundle includes a valid index.html file in its root folder.');
}

const entryFile = fs.readFileSync(Path.join(buildFolder, entry), { encoding: 'utf8' });

if (fileContainsAbsolutePath(entryFile)) {
console.log('----------------------------');
console.warn(
`${chalk.red(
'Warning:'
)} This bundle uses absolute paths. Please use relative paths instead for correct rendering`
);
Jwhiles marked this conversation as resolved.
Show resolved Hide resolved
console.log('----------------------------');
}
};

module.exports = {
validateBundle,
};
@@ -0,0 +1,45 @@
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const assert = require('assert');

describe('validateBundle', () => {
beforeEach(() => {
sinon.stub(console, 'warn');
});
afterEach(() => {
console.warn.restore();
});

it('throws when there is no index.html', () => {
const fsstub = { readdirSync: () => [] };
const { validateBundle } = proxyquire('./validate-bundle', { fs: fsstub });
try {
validateBundle('build');
} catch (e) {
assert.strictEqual(
e.message,
'Make sure your bundle includes a valid index.html file in its root folder.'
);
}
});

it('warns when the index.html contains an absolute path', () => {
const fsstub = {
readdirSync: () => ['index.html'],
readFileSync: () => '<html><script src="/absolute/path"></script></html>',
};
const { validateBundle } = proxyquire('./validate-bundle', { fs: fsstub });
validateBundle('build');
assert(console.warn.calledWith(sinon.match(/absolute paths/)));
});

it('does not warn when the index.html contains only relative paths', () => {
const fsstub = {
readdirSync: () => ['index.html'],
readFileSync: () => '<html><script src="./relative/path"></script></html>',
};
const { validateBundle } = proxyquire('./validate-bundle', { fs: fsstub });
validateBundle('build');
sinon.assert.notCalled(console.warn)
});
});