This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/allow-latest-tag
- Loading branch information
Showing
17 changed files
with
319 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,17 @@ | |
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. | ||
|
||
## [7.0.2](https://github.com/americanexpress/one-app-cli/compare/@americanexpress/[email protected]...@americanexpress/[email protected]) (2024-03-20) | ||
|
||
|
||
### Bug Fixes | ||
|
||
* **appConfig:** restore check to app config ([08f62d2](https://github.com/americanexpress/one-app-cli/commit/08f62d2e8c2eadb4a9751ffd7ac5c51553972e8e)) | ||
|
||
|
||
|
||
|
||
|
||
## [7.0.1](https://github.com/americanexpress/one-app-cli/compare/@americanexpress/[email protected]...@americanexpress/[email protected]) (2024-03-12) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
packages/one-app-bundler/__tests__/webpack/plugins/restrict-runtime-symbols.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
/* | ||
* Copyright 2024 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import fs from 'node:fs'; | ||
import { validateAppConfig } from '@americanexpress/one-app-dev-bundler'; | ||
import RestrictRuntimeSymbols from '../../../webpack/plugins/restrict-runtime-symbols.js'; | ||
|
||
jest.mock('@americanexpress/one-app-dev-bundler', () => ({ | ||
validateAppConfig: jest.fn(() => []), | ||
})); | ||
|
||
jest.mock('node:fs', () => ({ | ||
...jest.requireActual('node:fs'), | ||
readFileSync: jest.fn(() => ''), | ||
})); | ||
|
||
describe('RestrictRuntimeSymbols', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('should Register an assetEmitted tap hook', () => { | ||
const plugin = new RestrictRuntimeSymbols(); | ||
|
||
const mockCompiler = { | ||
hooks: { | ||
assetEmitted: { | ||
tap: jest.fn(), | ||
}, | ||
}, | ||
}; | ||
|
||
plugin.apply(mockCompiler); | ||
|
||
expect(mockCompiler.hooks.assetEmitted.tap).toHaveBeenCalledTimes(1); | ||
expect(mockCompiler.hooks.assetEmitted.tap.mock.calls[0][0]).toBe('RestrictRuntimeSymbols'); | ||
}); | ||
|
||
it('The registered function should not throw with a file containing no problems', () => { | ||
const plugin = new RestrictRuntimeSymbols(); | ||
|
||
let registeredFn; | ||
const mockCompiler = { | ||
hooks: { | ||
assetEmitted: { | ||
tap: jest.fn((_, fn) => { | ||
registeredFn = fn; | ||
}), | ||
}, | ||
}, | ||
}; | ||
|
||
fs.readFileSync.mockImplementationOnce(() => 'console.log(`no problems`)'); | ||
|
||
plugin.apply(mockCompiler); | ||
|
||
expect(registeredFn).not.toBe(undefined); | ||
|
||
expect(() => registeredFn('fileNameMock', { targetPath: 'target/Path/Mock.node.js' })).not.toThrow(); | ||
expect(() => registeredFn('fileNameMock', { targetPath: 'target/Path/Mock.browser.js' })).not.toThrow(); | ||
|
||
expect(fs.readFileSync).toHaveBeenCalledTimes(2); | ||
|
||
expect(mockCompiler.hooks.assetEmitted.tap.mock.calls[0][0]).toBe('RestrictRuntimeSymbols'); | ||
}); | ||
|
||
it('The registered function should throw if `create-react-class` is used in node bundles', () => { | ||
const plugin = new RestrictRuntimeSymbols(); | ||
|
||
let registeredFn; | ||
const mockCompiler = { | ||
hooks: { | ||
assetEmitted: { | ||
tap: jest.fn((_, fn) => { | ||
registeredFn = fn; | ||
}), | ||
}, | ||
}, | ||
}; | ||
|
||
fs.readFileSync.mockImplementationOnce(() => 'console.log(`create-react-class`)'); | ||
|
||
plugin.apply(mockCompiler); | ||
|
||
expect(registeredFn).not.toBe(undefined); | ||
|
||
expect(() => registeredFn('fileNameMock', { targetPath: 'target/Path/Mock.node.js' })).toThrow('`create-react-class` is restricted from being used'); | ||
|
||
expect(fs.readFileSync).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockCompiler.hooks.assetEmitted.tap.mock.calls[0][0]).toBe('RestrictRuntimeSymbols'); | ||
}); | ||
|
||
it('The registered function should throw if `create-react-class` is used in browser bundles', () => { | ||
const plugin = new RestrictRuntimeSymbols(); | ||
|
||
let registeredFn; | ||
const mockCompiler = { | ||
hooks: { | ||
assetEmitted: { | ||
tap: jest.fn((_, fn) => { | ||
registeredFn = fn; | ||
}), | ||
}, | ||
}, | ||
}; | ||
|
||
fs.readFileSync.mockImplementationOnce(() => 'console.log(`create-react-class`)'); | ||
|
||
plugin.apply(mockCompiler); | ||
|
||
expect(registeredFn).not.toBe(undefined); | ||
|
||
expect(() => registeredFn('fileNameMock', { targetPath: 'target/Path/Mock.browser.js' })).toThrow('`create-react-class` is restricted from being used'); | ||
|
||
expect(fs.readFileSync).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockCompiler.hooks.assetEmitted.tap.mock.calls[0][0]).toBe('RestrictRuntimeSymbols'); | ||
}); | ||
|
||
it('should call validateAppConfig with the file content if it containts `.appConfig`', () => { | ||
const plugin = new RestrictRuntimeSymbols(); | ||
|
||
let registeredFn; | ||
const mockCompiler = { | ||
hooks: { | ||
assetEmitted: { | ||
tap: jest.fn((_, fn) => { | ||
registeredFn = fn; | ||
}), | ||
}, | ||
}, | ||
}; | ||
|
||
fs.readFileSync.mockImplementationOnce(() => 'Module.appConfig = `someConfig`;'); | ||
|
||
plugin.apply(mockCompiler); | ||
|
||
expect(registeredFn).not.toBe(undefined); | ||
|
||
registeredFn('fileNameMock', { targetPath: 'target/Path/Mock.browser.js' }); | ||
|
||
expect(validateAppConfig).toHaveBeenCalledTimes(1); | ||
expect(validateAppConfig).toHaveBeenNthCalledWith(1, 'Module.appConfig = `someConfig`;'); | ||
|
||
expect(fs.readFileSync).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockCompiler.hooks.assetEmitted.tap.mock.calls[0][0]).toBe('RestrictRuntimeSymbols'); | ||
}); | ||
|
||
it('should thrown an error if validateAppConfig returns messages', () => { | ||
const plugin = new RestrictRuntimeSymbols(); | ||
|
||
let registeredFn; | ||
const mockCompiler = { | ||
hooks: { | ||
assetEmitted: { | ||
tap: jest.fn((_, fn) => { | ||
registeredFn = fn; | ||
}), | ||
}, | ||
}, | ||
}; | ||
|
||
validateAppConfig.mockImplementationOnce(() => ['Error Message Mock']); | ||
fs.readFileSync.mockImplementationOnce(() => 'Module.appConfig = `someConfig`;'); | ||
|
||
plugin.apply(mockCompiler); | ||
|
||
expect(registeredFn).not.toBe(undefined); | ||
|
||
expect(() => registeredFn('fileNameMock', { targetPath: 'target/Path/Mock.browser.js' })).toThrow('appConfig validation failed with the following messages: [ Error Message Mock ]'); | ||
|
||
expect(validateAppConfig).toHaveBeenCalledTimes(1); | ||
expect(validateAppConfig).toHaveBeenNthCalledWith(1, 'Module.appConfig = `someConfig`;'); | ||
|
||
expect(fs.readFileSync).toHaveBeenCalledTimes(1); | ||
|
||
expect(mockCompiler.hooks.assetEmitted.tap.mock.calls[0][0]).toBe('RestrictRuntimeSymbols'); | ||
}); | ||
|
||
it('should do nothing for other bundles', () => { | ||
const plugin = new RestrictRuntimeSymbols(); | ||
|
||
let registeredFn; | ||
const mockCompiler = { | ||
hooks: { | ||
assetEmitted: { | ||
tap: jest.fn((_, fn) => { | ||
registeredFn = fn; | ||
}), | ||
}, | ||
}, | ||
}; | ||
|
||
validateAppConfig.mockImplementationOnce(() => ['Error Message Mock']); | ||
fs.readFileSync.mockImplementationOnce(() => 'Module.appConfig = `create-react-class`;'); | ||
|
||
plugin.apply(mockCompiler); | ||
|
||
expect(registeredFn).not.toBe(undefined); | ||
|
||
registeredFn('fileNameMock', { targetPath: 'target/Path/Mock.other.js' }); | ||
|
||
expect(validateAppConfig).toHaveBeenCalledTimes(0); | ||
expect(fs.readFileSync).toHaveBeenCalledTimes(0); | ||
|
||
expect(mockCompiler.hooks.assetEmitted.tap.mock.calls[0][0]).toBe('RestrictRuntimeSymbols'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/one-app-bundler/webpack/plugins/restrict-runtime-symbols.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import fs from 'node:fs'; | ||
import { | ||
validateAppConfig, | ||
} from '@americanexpress/one-app-dev-bundler'; | ||
|
||
class RestrictRuntimeSymbols { | ||
// eslint-disable-next-line class-methods-use-this -- no need for this | ||
apply(compiler) { | ||
compiler.hooks.assetEmitted.tap('RestrictRuntimeSymbols', (file, { targetPath }) => { | ||
if (targetPath.endsWith('.node.js')) { | ||
const initialContent = fs.readFileSync(targetPath, 'utf8'); | ||
if (initialContent.match(/create-react-class/)) { | ||
throw new Error('`create-react-class` is restricted from being used'); | ||
} | ||
} else if (targetPath.endsWith('rowser.js')) { // catch browser and legacyBrowser | ||
const initialContent = fs.readFileSync(targetPath, 'utf8'); | ||
|
||
if (initialContent.match(/create-react-class/)) { | ||
throw new Error('`create-react-class` is restricted from being used'); | ||
} | ||
|
||
if (initialContent.match(/.appConfig/)) { | ||
const messages = []; | ||
validateAppConfig(initialContent).forEach((message) => messages.push(message)); | ||
if (messages.length > 0) { | ||
throw new Error(`appConfig validation failed with the following messages: [ ${messages.join('/n,')} ]`); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default RestrictRuntimeSymbols; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,17 @@ | |
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. | ||
|
||
## [1.7.2](https://github.com/americanexpress/one-app-cli/compare/@americanexpress/[email protected]...@americanexpress/[email protected]) (2024-03-20) | ||
|
||
|
||
### Bug Fixes | ||
|
||
* **appConfig:** restore check to app config ([08f62d2](https://github.com/americanexpress/one-app-cli/commit/08f62d2e8c2eadb4a9751ffd7ac5c51553972e8e)) | ||
|
||
|
||
|
||
|
||
|
||
## [1.7.1](https://github.com/americanexpress/one-app-cli/compare/@americanexpress/[email protected]...@americanexpress/[email protected]) (2024-03-12) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.