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

Adjustments to support Gasket in Next Edge Runtime #961

Merged
merged 9 commits into from
Nov 12, 2024
Merged
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
22 changes: 2 additions & 20 deletions package-lock.json

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
@@ -1,7 +1,7 @@
import path from 'path';
import os from 'os';
import action from '../action-wrapper.js';
import { default as gasketUtils } from '@gasket/utils';
import { PackageManager } from '@gasket/utils';
import { mkdtemp } from 'fs/promises';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
Expand All @@ -16,7 +16,7 @@ async function loadPresets({ context }) {
context.tmpDir = tmpDir;

const modPath = path.join(tmpDir, 'node_modules');
const pkgManager = new gasketUtils.PackageManager({
const pkgManager = new PackageManager({
packageManager: context.packageManager,
dest: tmpDir
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import action from '../action-wrapper.js';
import { ConfigBuilder } from '../config-builder.js';
import { default as gasketUtils } from '@gasket/utils';
import { PackageManager } from '@gasket/utils';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const { dependencies } = require('../../../package.json');
Expand All @@ -25,7 +25,7 @@ async function setupPkg({ context }) {
'@gasket/utils': dependencies['@gasket/utils']
});

const pkgManager = new gasketUtils.PackageManager(context);
const pkgManager = new PackageManager(context);
Object.assign(context, { pkg, pkgManager });
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest } from '@jest/globals';
const mockExecStub = jest.fn();

const postCreateHooks = (await import('../../../../lib/scaffold/actions/post-create-hooks')).default;
const postCreateHooks = (await import('../../../../lib/scaffold/actions/post-create-hooks.js')).default;

describe('postCreateHooks', () => {
let mockContext, mockGasket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jest.mock('@gasket/utils', () => ({
PackageManager: MockPackageManager
}));

const setupPkg = (await import('../../../../lib/scaffold/actions/setup-pkg')).default;
const setupPkg = (await import('../../../../lib/scaffold/actions/setup-pkg.js')).default;
const { ConfigBuilder } = await import('../../../../lib/scaffold/config-builder');


Expand Down
3 changes: 3 additions & 0 deletions packages/gasket-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `@gasket/core`

- Edge runtime support using lean export from `@gasket/utils` ([#961])

### 7.0.6

- Fix to include `@gasket/utils` as a dependency ([#949])
Expand All @@ -12,3 +14,4 @@
[Version 7 Upgrade Guide]: /docs/upgrade-to-7.md

[#949]: https://github.com/godaddy/gasket/pull/949
[#961]: https://github.com/godaddy/gasket/pull/961
13 changes: 8 additions & 5 deletions packages/gasket-core/lib/gasket.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console, no-process-env */

import { GasketEngine, lifecycleMethods } from './engine.js';
import { applyConfigOverrides } from '@gasket/utils';
import { applyConfigOverrides } from '@gasket/utils/config';
import { makeTraceBranch } from './trace.js';

/**
Expand All @@ -23,6 +23,7 @@ function getEnvironment() {

/**
* The Gasket class is the main entry point for the Gasket API.
* @type {import('@gasket/core').Gasket}
*/
export class Gasket {

Expand All @@ -31,14 +32,16 @@ export class Gasket {
*/
constructor(configDef) {
const env = getEnvironment();
/** @type {import('@gasket/core').GasketConfig} */
const config = applyConfigOverrides(configDef, { env });
config.env = env;
config.root ??= process.cwd();

// prune nullish and/or empty plugins
config.plugins = config.plugins
.filter(Boolean)
.map(plugin => plugin.default || plugin) // quality of life for cjs apps
// @ts-ignore - default not expected - quality of life for cjs apps
.map(plugin => plugin.default || plugin)
.filter(plugin => Boolean(plugin.name) || Boolean(plugin.hooks));

// start the engine
Expand All @@ -58,11 +61,11 @@ export class Gasket {
// Can be used as a key to identify a gasket instance
this.symbol = Symbol('gasket');

// @ts-ignore
// @ts-ignore - attached lifecycle trace methods
this.execSync('init');
// @ts-ignore
// @ts-ignore - attached lifecycle trace methods
this.config = this.execWaterfallSync('configure', config);
// @ts-ignore
// @ts-ignore - attached lifecycle trace methods
this.exec('ready');
}

Expand Down
8 changes: 4 additions & 4 deletions packages/gasket-core/lib/trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class GasketTrace {
* Wrap a lifecycle function to trace start and end.
* A GasketTrace instance is passed to the lifecycle function
* to allow for tracing and further branching.
* @type {import('./internal').isolateLifecycle<any>}
* @type {import('./internal.js').isolateLifecycle<any>}
*/
function isolateLifecycle(source, name, fn) {
const instance = new GasketTrace(source._proxy);
Expand All @@ -145,7 +145,7 @@ function isolateLifecycle(source, name, fn) {
* Wrap an action function to trace start and end.
* A GasketTrace instance is passed to the action function
* to allow for tracing and further branching.
* @type {import('./internal').isolateAction<any>}
* @type {import('./internal.js').isolateAction<any>}
*/
function isolateAction(source, name, fn) {
const instance = new GasketTrace(source._proxy);
Expand All @@ -170,7 +170,7 @@ function isolateAction(source, name, fn) {
/**
* Create a proxy of actions to intercept the functions
* and return a traceable version.
* @type {import('./internal').interceptActions}
* @type {import('./internal.js').interceptActions}
*/
function interceptActions(source, actions) {
return new Proxy(actions, {
Expand All @@ -185,7 +185,7 @@ function interceptActions(source, actions) {

/**
* Create a new GasketTrace instance from a Gasket.
* @type {import('./internal').makeTraceBranch}
* @type {import('./internal.js').makeTraceBranch}
*/
export function makeTraceBranch(gasket) {
const instance = new GasketTrace(gasket, GasketTrace._nextBranchId++);
Expand Down
1 change: 1 addition & 0 deletions packages/gasket-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"typecheck": "tsc",
"typecheck:watch": "tsc --watch",
"build": "swc lib -d cjs --delete-dir-on-start --strip-leading-paths",
"build:watch": "npm run build -- --watch",
"postbuild": "node -e \"require('fs').writeFileSync('cjs/package.json', '{}')\"",
"prepublishOnly": "npm run build"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/gasket-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"compilerOptions": {
"moduleResolution": "NodeNext",
"module": "NodeNext",
"downlevelIteration": true,
"allowJs": true,
"checkJs": true,
Expand Down
6 changes: 3 additions & 3 deletions packages/gasket-plugin-data/lib/configure.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { applyConfigOverrides } = require('@gasket/utils');
const { applyConfigOverrides } = require('@gasket/utils/config');
const { baseDataMap } = require('./actions');

/**
* @type {import('@gasket/core').HookHandler<'configure'>}
*/
function configure(gasket, baseConfig) {
const { command, config: { env, root } } = gasket;
const { command, config: { env } } = gasket;
const commandId = command && command.id;
if ('data' in baseConfig) {
const data = applyConfigOverrides(
baseConfig.data,
{ env, commandId, root }
{ env, commandId }
);

baseDataMap.set(gasket.symbol, data);
Expand Down
4 changes: 3 additions & 1 deletion packages/gasket-plugin-data/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"compilerOptions": {
"moduleResolution": "bundler",
"module": "preserve",
"allowJs": true,
"checkJs": true,
"noEmit": true,
Expand All @@ -20,4 +22,4 @@
"coverage",
"generator"
]
}
}
1 change: 1 addition & 0 deletions packages/gasket-plugin-git/lib/gitignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = class Gitignore {
}

/**
* TODO: convert to jsdocs imports
* Adds content to gitignore
* @param {string | string[]} name - name of file or directory to add to
* gitignore
Expand Down
2 changes: 2 additions & 0 deletions packages/gasket-plugin-git/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"compilerOptions": {
"moduleResolution": "bundler",
"module": "preserve",
"allowJs": true,
"checkJs": true,
"noEmit": true,
Expand Down
3 changes: 2 additions & 1 deletion packages/gasket-typescript-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"module": "preserve",
"moduleResolution": "bundler", /* Specify what module code is generated. */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
"noEmit": true, /* Disable emitting files from a compilation. */
Expand Down
5 changes: 5 additions & 0 deletions packages/gasket-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# `@gasket/utils`

- Adjustments to support Gasket in edge runtime ([#961])
- Add config export for leaner applyConfigOverrides
- Use `deepmerge` instead of `lodash.defaultsdeep` due to incompatible _new Function_

### 7.0.0

- See [Version 7 Upgrade Guide] for overall changes
Expand Down Expand Up @@ -91,3 +95,4 @@
[#607]: https://github.com/godaddy/gasket/pull/607
[#647]: https://github.com/godaddy/gasket/pull/647
[#768]: https://github.com/godaddy/gasket/pull/768
[#961]: https://github.com/godaddy/gasket/pull/961
59 changes: 59 additions & 0 deletions packages/gasket-utils/lib/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// This file is exported by the package and can be imported directly.

interface ConfigContext {
/** Name of environment */
env: string;
/** Name of command */
commandId?: string;
/** Project root; required if using localeFile */
}

interface ConfigDefinition extends Record<string, any> {
environments?: Record<string, Partial<ConfigDefinition>>
commands?: Record<string, Partial<ConfigDefinition>>
[key: string]: any
}

type ConfigOutput = Omit<ConfigDefinition, 'environments' | 'commands'>
type ConfigPartial = Partial<ConfigDefinition> | undefined | void | unknown;

export function getPotentialConfigs(
config: ConfigDefinition,
configContext: ConfigContext
): Array<ConfigPartial>;

function getCommandOverrides(
commands: Record<string, Partial<ConfigDefinition>>,
commandId: string
): Array<ConfigPartial>;

function getSubEnvironmentOverrides(
env: string,
environments: Record<string, Partial<ConfigDefinition>>
): Array<ConfigPartial>;

function getDevOverrides(
isLocalEnv: boolean,
environments: Record<string, Partial<ConfigDefinition>>
): Array<ConfigPartial>;

async function getLatestVersion(
pkgName: string,
/** current time in milliseconds */
currentTime: number,
cache: Record<string, any>
): Promise<string>;

function getLocalOverrides(
isLocalEnv: boolean,
root: string,
localFile: string
): Generator<ConfigDefinition | undefined, void, unknown>;

/**
* Normalize the config by applying any overrides for environments, commands, or local-only config file.
*/
export function applyConfigOverrides<Def extends ConfigDefinition, Out extends ConfigOutput>(
config: Def,
configContext: ConfigContext
): Out;
Loading
Loading