Skip to content

Commit

Permalink
Add remove to config builder (#1036)
Browse files Browse the repository at this point in the history
* feat: add remove to config builder

* chore: update changelog

* chore: update changelog
  • Loading branch information
jpina1-godaddy authored Feb 24, 2025
1 parent c9a5607 commit cf4b27f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/create-gasket-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# `create-gasket-app`

- Add `remove` to config-builder ([#1036])
- Add `addCommand` method for create context ([#1034])
- Add `addEnvironment` method for create context ([#1010])

Expand All @@ -11,7 +12,7 @@

- Aligned version releases across all packages

Added `@gasket/plugin-dynamic-plugin` to default plugins ([#970])
- Added `@gasket/plugin-dynamic-plugin` to default plugins ([#970])

### 7.0.6

Expand Down Expand Up @@ -94,3 +95,4 @@ Added `@gasket/plugin-dynamic-plugin` to default plugins ([#970])
[#1010]: https://github.com/godaddy/gasket/pull/1010
[#1014]: https://github.com/godaddy/gasket/pull/1014
[#1034]: https://github.com/godaddy/gasket/pull/1034
[#1036]: https://github.com/godaddy/gasket/pull/1036
5 changes: 5 additions & 0 deletions packages/create-gasket-app/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export interface ConfigBuilder<Config> {
): void;
add(key: string, value: object, options?: object): void;

/** Remove a key from fields
* @param {string[]} path - Array of strings representing the path to the field to remove
*/
remove(path: string[]): void;

/**
* addPlugin - Add plugin import to the gasket file and use the value in the plugins array
* @param {string} pluginImport - name of the import used as a value - `import pluginImport...`
Expand Down
23 changes: 23 additions & 0 deletions packages/create-gasket-app/lib/scaffold/config-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,29 @@ export class ConfigBuilder {
}
}

/** Remove a key from fields
* @param {string[]} path - Array of strings representing the path to the field to remove
*/
remove(path) {
if (!Array.isArray(path) || path.length === 0) {
throw new Error('Path must be a non-empty array of strings');
}

let current = this.fields;
for (let i = 0; i < path.length - 1; i++) {
if (Object.prototype.hasOwnProperty.call(current, path[i])) {
current = current[path[i]];
} else {
return; // Path does not exist
}
}

const key = path[path.length - 1];
if (Object.prototype.hasOwnProperty.call(current, key)) {
delete current[key];
}
}

/**
* addPlugin - Add plugin import to the gasket file and use the value in the plugins array
* @param {string} pluginImport - name of the import used as a value - `import pluginImport...`
Expand Down
3 changes: 3 additions & 0 deletions packages/create-gasket-app/lib/scaffold/create-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ function makeCreateRuntime(context, source) {

has(key, value) {
return context.pkg.has(key, value);
},
remove(path) {
context.pkg.remove(path);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,22 @@ describe('ConfigBuilder', () => {
});
});

describe('.remove(path)', () => {
it('removes a field', () => {
pkg.add('name', 'my-app');
expect(pkg.fields).toHaveProperty('name', 'my-app');
pkg.remove(['name']);
expect(pkg.fields).not.toHaveProperty('name');
});

it('removes a nested field', () => {
pkg.add('dependencies', { 'some-pkg': 'latest' });
expect(pkg.fields.dependencies).toHaveProperty('some-pkg');
pkg.remove(['dependencies', 'some-pkg']);
expect(pkg.fields.dependencies).not.toHaveProperty('some-pkg');
});
});

describe('.toOrderedKeys(obj, [orderBy])', () => {
it('should order lexographically by default', () => {
const ordered = pkg.toOrderedKeys({
Expand Down

0 comments on commit cf4b27f

Please sign in to comment.