Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #30 from blackbaud/add-static-client-flag
Browse files Browse the repository at this point in the history
add ability to run deploy on static client
  • Loading branch information
Bobby Earl authored Feb 6, 2019
2 parents 3e872b7 + 4927226 commit 03d85b8
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ All of the following options are required and overridable via the CLI. For exam
| `azureStorageAccount` | `null` |
| `azureStorageAccessKey` | `null` |
| `azureStorageTableName` | "spa" |
| `isStaticClient` | `false` |

`null` values are typically supplied by the CI process.
75 changes: 59 additions & 16 deletions lib/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const glob = require('glob');
const cwd = process.cwd();
const dist = path.join(cwd, 'dist');
const assets = path.join(dist, 'assets');
const bundles = path.join(dist, 'bundles');
const sriAlgorithm = 'sha384';
const readOptions = { encoding: 'utf8' };

Expand All @@ -29,33 +30,75 @@ const getEmittedAssets = () => glob
* Returns an array of assets (files + metadata).
* @name getDistAssets
* @param {boolean} includeContent
* @param {boolean} isStaticClient
* @returns {Array} assets
*/
const getDistAssets = (includeContent) => {
const metadataPath = path.join(dist, 'metadata.json');
const getDistAssets = (includeContent, isStaticClient, version) => {
let metadataPath = path.join(dist, 'metadata.json');

if (isStaticClient) {
return glob.sync(path.join(bundles, '*.js'))
.map(bundle => formatAssets(
includeContent,
isStaticClient,
{
name: path.basename(bundle),
version: version
}
));
}

if (fs.existsSync(metadataPath)) {
const metadata = require(metadataPath);
return metadata.map((asset) =>
formatAssets(includeContent, isStaticClient, asset));
}

return metadata.map((asset) => {
const parsed = path.parse(asset.name);
const content = fs.readFileSync(path.join(dist, asset.name), readOptions);
const hash = getHash(content, 'md5', 'hex');
return [];
};

let newProperties = {
name: parsed.name + '.' + hash + parsed.ext,
sri: sriAlgorithm + '-' + getHash(content, sriAlgorithm, 'base64')
};
/**
* Formats individual assets
* @name formatAssets
* @param {boolean} includeContent
* @param {boolean} isStaticClient
* @param {object} asset
* @returns {object} asset
*/
const formatAssets = (includeContent, isStaticClient, asset) => {
const parsed = path.parse(asset.name);

if (includeContent) {
newProperties.content = content;
}
let content;
if (isStaticClient) {
content = fs.readFileSync(
path.join(bundles, asset.name),
readOptions);
} else {
content = fs.readFileSync(
path.join(dist, asset.name),
readOptions);
}

const hash = getHash(content, 'md5', 'hex');

return merge({}, asset, newProperties);
});
let name;

if (isStaticClient) {
name = asset.version + '/' + parsed.base;
} else {
name = parsed.name + '.' + hash + parsed.ext;
}

return [];
let newProperties = {
name: name,
sri: sriAlgorithm + '-' + getHash(content, sriAlgorithm, 'base64')
};

if (includeContent) {
newProperties.content = content;
}

return merge({}, asset, newProperties);
};

/**
Expand Down
12 changes: 10 additions & 2 deletions lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ const assets = require('./assets');
const azure = require('./azure');

module.exports = (settings) => {
const assetsWithContent = assets.getDistAssets(true);
const assetsWithoutContent = assets.getDistAssets(false);
const assetsWithContent = assets.getDistAssets(
true,
settings.isStaticClient,
settings.version
);
const assetsWithoutContent = assets.getDistAssets(
false,
settings.isStaticClient,
settings.version
);
const assetsEmitted = assets.getEmittedAssets();
const assetsCombined = assetsWithContent.concat(assetsEmitted);

Expand Down
6 changes: 4 additions & 2 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ function getLocalConfig(filename) {
* @name getSettings
* @returns {Object} settings
*/
function getSettings(argv) {
function getSettings(argv = {}) {

const packageConfig = getLocalConfig('package.json');
const skyuxConfig = getLocalConfig('skyuxconfig.json');
const logSkyuxVersion = argv.logSkyuxVersion !== 'false' && argv.logSkyuxVersion !== false;

const defaults = {
name: '',
version: '',
packageConfig: packageConfig,
skyuxConfig: skyuxConfig,
azureStorageTableName: 'spa'
azureStorageTableName: 'spa',
isStaticClient: false
};

// Merges in all packageConfig, really only care about name + version
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ function validate(assets, settings) {
'version',
'azureStorageAccount',
'azureStorageAccessKey',
'azureStorageTableName'
'azureStorageTableName',
'isStaticClient'
];
let success = true;

Expand Down
28 changes: 28 additions & 0 deletions test/lib-assets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('skyux-deploy lib assets', () => {
const readFileSync = fs.readFileSync;
spyOn(fs, 'existsSync').and.returnValue(true);
spyOn(fs, 'readFileSync').and.callFake((file, options) => {

if (file.indexOf('custom-name.js') > -1) {
return 'my-custom-content3';
} else {
Expand All @@ -106,6 +107,33 @@ describe('skyux-deploy lib assets', () => {
expect(assetsWithoutContent[0].content).not.toBeDefined();
});

it('should evaluate static client assets', () => {
const readFileSync = fs.readFileSync;

spyOn(glob, 'sync').and.returnValue([
path.join(process.cwd(), 'dist', 'bundles', 'test.umd.js')
]);
spyOn(fs, 'readFileSync').and.callFake((file, options) => {
if (file.indexOf('test.umd.js') > -1) {
return 'my-custom-content3';
} else {
return readFileSync(file, options);
}
});

let stubs = {};
stubs[path.join(process.cwd(), 'dist', 'bundles', 'test.umd.js')] = [
{
name: 'custom-name.js'
}
];

const lib = proxyquire('../lib/assets', stubs);

const assetsWithContent = lib.getDistAssets(true, true);

expect(assetsWithContent[0].content).toEqual('my-custom-content3');
});
});

describe('getEmittedAssets', () => {
Expand Down
5 changes: 2 additions & 3 deletions test/lib-settings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('skyux-deploy lib settings', () => {
expect(settings.version).toEqual('');
});

function setupPackageJson(_requested) {
function setupPackageJson(_requested, argv) {
const skyuxInstalledPath = path.join(
process.cwd(),
'node_modules',
Expand All @@ -78,10 +78,9 @@ describe('skyux-deploy lib settings', () => {
});

const lib = require('../lib/settings');
const settings = lib.getSettings();
const settings = lib.getSettings(argv);

expect(logger.error).not.toHaveBeenCalled();
return settings;
}

});

0 comments on commit 03d85b8

Please sign in to comment.