Skip to content

Commit

Permalink
refactor: samples generation
Browse files Browse the repository at this point in the history
OKTA-474183
<<<Jenkins Check-In of Tested SHA: 631473d for [email protected]>>>
Artifact: okta-auth-js
Files changed count: 99
PR Link: "#1122"
  • Loading branch information
shuowu authored and eng-prod-CI-bot-okta committed Feb 26, 2022
1 parent 550944e commit 5c53f02
Show file tree
Hide file tree
Showing 99 changed files with 97 additions and 5,213 deletions.
1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

13 changes: 13 additions & 0 deletions samples/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generated/static-spa/**/* linguist-generated=true
generated/webpack-spa/**/* linguist-generated=true
generated/express-web-no-oidc/**/* linguist-generated=true
generated/express-web-with-oidc/**/* linguist-generated=true
generated/express-embedded-auth-with-sdk/README.md linguist-generated=true
generated/express-embedded-auth-with-sdk/env linguist-generated=true
generated/express-embedded-auth-with-sdk/env/okta-env.js linguist-generated=true
generated/express-embedded-sign-in-widget/env/okta-env.js linguist-generated=true
generated/express-embedded-sign-in-widget/README.md linguist-generated=true
generated/express-embedded-sign-in-widget/env linguist-generated=true
generated/express-embedded-auth-with-sdk/env/index.js linguist-generated=true
generated/express-embedded-sign-in-widget/package.json linguist-generated=true
generated/express-embedded-sign-in-widget/env/index.js linguist-generated=true
17 changes: 16 additions & 1 deletion samples/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

const GENERATE_TYPE_FULL = 'full';
const GENERATE_TYPE_OVERWRITE = 'overwrite';

const defaults = {
port: '8080'
Expand Down Expand Up @@ -50,33 +52,38 @@ const samples = [
name: 'static-spa',
pkgName: '@okta/samples.static-spa',
template: 'static-spa',
generateType: GENERATE_TYPE_FULL,
specs: ['spa-app'],
features: []
},
{
name: 'webpack-spa',
pkgName: '@okta/samples.webpack-spa',
template: 'webpack-spa',
generateType: GENERATE_TYPE_FULL,
specs: ['spa-app'],
features: []
},
{
name: 'express-web-no-oidc',
pkgName: '@okta/samples.express.web-no-oidc',
template: 'express-web',
generateType: GENERATE_TYPE_FULL,
specs: ['web-app'],
oidc: false
},
{
name: 'express-web-with-oidc',
pkgName: '@okta/samples.express.web-with-oidc',
template: 'express-web',
generateType: GENERATE_TYPE_FULL,
specs: ['web-app']
},
{
name: 'express-embedded-auth-with-sdk',
pkgName: '@okta/samples.express.embedded-auth-with-sdk',
template: 'express-embedded-auth-with-sdk',
generateType: GENERATE_TYPE_OVERWRITE,
specs: ['express-embedded-auth-with-sdk'],
features: [
'root-page',
Expand All @@ -98,6 +105,7 @@ const samples = [
name: 'express-embedded-sign-in-widget',
pkgName: '@okta/samples.express.embedded-sign-in-widget',
template: 'express-embedded-sign-in-widget',
generateType: GENERATE_TYPE_OVERWRITE,
specs: [],
features: [
'embedded-widget-basic-auth',
Expand All @@ -122,6 +130,10 @@ function getSampleConfig(sampleName) {
return sampleConfig;
}

function getSamplesConfig() {
return samples;
}

function getSampleNames() {
return samples.map(sample => sample.name).filter(name => {
if (process.env.SAMPLE_NAME) {
Expand All @@ -133,5 +145,8 @@ function getSampleNames() {

module.exports = {
getSampleNames,
getSampleConfig
getSampleConfig,
getSamplesConfig,
GENERATE_TYPE_FULL,
GENERATE_TYPE_OVERWRITE
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ router.get('/login', async (req, res, next) => {
otp
};
res.render('login', {
siwVersion: '6.1.0',
siwVersion: process.env.SIW_VERSION,
widgetConfig: JSON.stringify(widgetConfig),
selfHosted: !!process.env.SELF_HOSTED_WIDGET
});
Expand Down
83 changes: 63 additions & 20 deletions samples/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,41 @@ const rename = require('gulp-rename');
const shell = require('shelljs');
const merge = require('merge-stream');
const path = require('path');
const fs = require('fs');

const config = require('./config');
const {
getSampleNames,
getSampleConfig,
GENERATE_TYPE_FULL,
GENERATE_TYPE_OVERWRITE
} = require('./config');

Handlebars.registerHelper('append', (str) => str);

const SRC_DIR = 'templates';
const BUILD_DIR = 'generated';
const PARTIALS_DIR = `${SRC_DIR}/partials`;
const OKTA_ENV_SCRIPT_PATH = '../env/index.js';
const GIT_ATTRIBUTES_PATH = '.gitattributes';

const versionsCache = {
siwVersion: process.env.SIW_VERSION || getPublishedModuleVersion('@okta/okta-signin-widget'),
authJSVersion: getPublishedModuleVersion('@okta/okta-auth-js')
};
const gitattributes = (() => {
const arr = fs.readFileSync(GIT_ATTRIBUTES_PATH)
.toString()
.split('\n')
.map(str => str.trim())
.filter(str => !!str);
return new Set(arr);
})();

function cleanTask() {
const dirs = config.getSampleNames().map(name => path.join(BUILD_DIR, name));
const dirs = getSampleNames()
.map(name => getSampleConfig(name))
.filter(config => config.generateType === GENERATE_TYPE_FULL)
.map(({ name }) => path.join(BUILD_DIR, name));
return src(dirs, { read: false, allowEmpty: true })
.pipe(clean({ force: true }));
}
Expand All @@ -52,9 +77,8 @@ function registerPartials() {

function generateSampleTaskFactory(options) {
return function generateSample() {
const { name, template, subDir, useEnv } = options;
const { name, template, subDir, useEnv, generateType } = options;
const inDir = `${SRC_DIR}/${template}/**/*`;
const viewTemplatesDir = `${SRC_DIR}/${template}/**/views/*`;
const outDir = `${BUILD_DIR}/` + (subDir ? `${subDir}/` : '') + `${name}`;
const strOptions = {};
Object.keys(options).forEach(key => {
Expand All @@ -65,45 +89,64 @@ function generateSampleTaskFactory(options) {
strOptions[key] = val;
});

const hbParams = Object.assign({}, strOptions, {
siwVersion: process.env.SIW_VERSION || getPublishedModuleVersion('@okta/okta-signin-widget'),
authJSVersion: getPublishedModuleVersion('@okta/okta-auth-js')
});
if (generateType === GENERATE_TYPE_FULL) {
gitattributes.add(`${outDir}/**/* linguist-generated=true`);
}

const hbParams = Object.assign({}, strOptions, versionsCache);
console.log(`generating sample: "${name}"`, hbParams);
const generateWithoutViewTemplates = src([inDir, `!${viewTemplatesDir}`], { dot: true })
const generateTemplates = src(inDir, { dot: true })
.pipe(handlebars(hbParams))
.pipe(dest(outDir));
const copyViewTemplates = src(viewTemplatesDir, { dot: true })
.pipe(dest(outDir));
const merged = merge(generateWithoutViewTemplates, copyViewTemplates);
.pipe(dest((file) => {
if (generateType === GENERATE_TYPE_OVERWRITE) {
gitattributes.add(`${outDir}${file.path.split(name)[1]} linguist-generated=true`);
}
return outDir;
}));
const merged = merge(generateTemplates);
if (useEnv) {
const copyEnvModule = src(OKTA_ENV_SCRIPT_PATH, { dot: true })
.pipe(rename('okta-env.js'))
.pipe(dest(`${outDir}/env/`));
.pipe(dest(() => {
gitattributes.add(`${outDir}/env/okta-env.js linguist-generated=true`);
return `${outDir}/env/`;
}));
merged.add(copyEnvModule);
}
return merged;
};
}

const generateSampleTasks = config.getSampleNames().map(function(sampleName) {
const sampleConfig = config.getSampleConfig(sampleName);
const generateSampleTasks = getSampleNames().map(function(sampleName) {
const sampleConfig = getSampleConfig(sampleName);
const taskFn = generateSampleTaskFactory(sampleConfig);
task(sampleName, series(registerPartials, taskFn));
task(sampleConfig.name, series(registerPartials, taskFn));
return taskFn;
});
const buildSamples = parallel.apply(null, generateSampleTasks);

function writeGitAttributes(done) {
fs.writeFileSync(
GIT_ATTRIBUTES_PATH,
Array.from(gitattributes).reduce((acc, curr) => {
acc += curr + '\n';
return acc;
}, '')
);
done();
}

const defaultTask = series(
cleanTask,
registerPartials,
buildSamples
buildSamples,
writeGitAttributes
);

function watchSamples() {
watch([`${PARTIALS_DIR}/**/*`, `config.js`], defaultTask);
config.getSampleNames().map(function(sampleName) {
const sampleConfig = config.getSampleConfig(sampleName);
getSampleNames().map(function(sampleName) {
const sampleConfig = getSampleConfig(sampleName);
const { template } = sampleConfig;
const task = generateSampleTaskFactory(sampleConfig);
watch(`${SRC_DIR}/${template}/**/*`, task);
Expand Down
17 changes: 0 additions & 17 deletions samples/templates/express-embedded-auth-with-sdk/.eslintrc.js

This file was deleted.

1 change: 0 additions & 1 deletion samples/templates/express-embedded-auth-with-sdk/.npmrc

This file was deleted.

65 changes: 0 additions & 65 deletions samples/templates/express-embedded-auth-with-sdk/config.js

This file was deleted.

26 changes: 0 additions & 26 deletions samples/templates/express-embedded-auth-with-sdk/package.json

This file was deleted.

Loading

0 comments on commit 5c53f02

Please sign in to comment.