Skip to content

Commit

Permalink
Add dot paths support (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza authored May 1, 2024
1 parent 7c77d22 commit 06848e0
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:
sources: |
**/*.json;!**/*.DEV.json;!**/vars.json => _tmp/*.json
**/*.xml;!**.dev.xml => _tmp/*.xml
**/*.YML => _tmp/*.yml
**/*.YML;!**/*vars.yml => _tmp/*.yml
variables: >
[
${{ toJSON(vars) }},
Expand All @@ -107,6 +107,7 @@ jobs:
recursive: true
transforms: true
case-insensitive-paths: true
include-dot-paths: true
env:
ENV_VARS: '{ "var4": "env_value4" }'

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## v1.2.0
- Upgrade package `@qetza/replacetokens` to `1.7.0`.
- Add support for case insensitive path matching in _sources_ and _variables_.
- Add support for matching directories and files starting with a dot in _sources_ and _variables_.

## v1.1.2
- Change telemetry provider.
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ Please refer to the [release page](https://github.com/qetza/replacetokens-action
# Optional. Default: ignore
if-no-files-found: ''

# Include directories and files starting with a dot '.' in glob matching results for sources
# and additionalVariables.
#
# Optional. Default: false
include-dot-paths: ''

# The log level.
#
# Accepted values:
Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ inputs:
Accepted values:
- auto: detect encoding using js-chardet
- any value supported by iconv-lite
default: auto
default: 'auto'
escape:
description: >
Character escape type to apply on each value.
Expand All @@ -71,6 +71,9 @@ inputs:
if-no-files-found:
description: 'The behavior if no files are found: ignore, warn, error.'
default: 'ignore'
include-dot-paths:
description: 'Include directories and files starting with a dot ''.'' in glob matching results for sources and additionalVariables.'
default: 'false'
log-level:
description: >
The log level.
Expand Down
9 changes: 6 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63665,7 +63665,8 @@ async function run() {
recursive: core.getBooleanInput('recursive'),
root: core.getInput('root'),
sources: {
caseInsensitive: core.getBooleanInput('case-insensitive-paths')
caseInsensitive: core.getBooleanInput('case-insensitive-paths'),
dot: core.getBooleanInput('include-dot-paths')
},
token: {
pattern: getChoiceInput('token-pattern', [
Expand Down Expand Up @@ -63717,7 +63718,7 @@ async function run() {
};
// load variables
const separator = core.getInput('separator') || rt.Defaults.Separator;
const variables = await getVariables(options.root, separator, options.sources.caseInsensitive);
const variables = await getVariables(options.root, separator, options.sources.caseInsensitive, options.sources.dot);
// set telemetry attributes
telemetryEvent.setAttributes({
sources: sources.length,
Expand All @@ -63728,6 +63729,7 @@ async function run() {
escape: options.escape.type,
'escape-char': options.escape.escapeChar,
'if-no-files-found': ifNoFilesFound,
'include-dot-paths': options.sources.dot,
'log-level': logLevelStr,
'missing-var-action': options.missing.action,
'missing-var-default': options.missing.default,
Expand Down Expand Up @@ -63806,12 +63808,13 @@ function getSources() {
var variableFilesCount = 0;
var variablesEnvCount = 0;
var inlineVariablesCount = 0;
async function getVariables(root, separator, caseInsensitive) {
async function getVariables(root, separator, caseInsensitive, dot) {
const input = core.getInput('variables', { required: true, trimWhitespace: true }) || '';
if (!input)
return {};
return await rt.loadVariables(getVariablesFromJson(input), {
caseInsensitive: caseInsensitive,
dot: dot,
normalizeWin32: true,
root: root,
separator: separator
Expand Down
15 changes: 12 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export async function run(): Promise<void> {
recursive: core.getBooleanInput('recursive'),
root: core.getInput('root'),
sources: {
caseInsensitive: core.getBooleanInput('case-insensitive-paths')
caseInsensitive: core.getBooleanInput('case-insensitive-paths'),
dot: core.getBooleanInput('include-dot-paths')
},
token: {
pattern:
Expand Down Expand Up @@ -116,7 +117,12 @@ export async function run(): Promise<void> {

// load variables
const separator = core.getInput('separator') || rt.Defaults.Separator;
const variables = await getVariables(options.root, separator, options.sources!.caseInsensitive);
const variables = await getVariables(
options.root,
separator,
options.sources!.caseInsensitive,
options.sources!.dot
);

// set telemetry attributes
telemetryEvent.setAttributes({
Expand All @@ -128,6 +134,7 @@ export async function run(): Promise<void> {
escape: options.escape!.type,
'escape-char': options.escape!.escapeChar,
'if-no-files-found': ifNoFilesFound,
'include-dot-paths': options.sources!.dot,
'log-level': logLevelStr,
'missing-var-action': options.missing!.action,
'missing-var-default': options.missing!.default,
Expand Down Expand Up @@ -219,13 +226,15 @@ var inlineVariablesCount = 0;
async function getVariables(
root?: string,
separator?: string,
caseInsensitive?: boolean
caseInsensitive?: boolean,
dot?: boolean
): Promise<{ [key: string]: string }> {
const input = core.getInput('variables', { required: true, trimWhitespace: true }) || '';
if (!input) return {};

return await rt.loadVariables(getVariablesFromJson(input), {
caseInsensitive: caseInsensitive,
dot: dot,
normalizeWin32: true,
root: root,
separator: separator
Expand Down
File renamed without changes.
65 changes: 51 additions & 14 deletions tests/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ describe('run', () => {
recursive: false,
root: '',
sources: {
caseInsensitive: false
caseInsensitive: false,
dot: false
},
token: {
pattern: rt.TokenPatterns.Default,
Expand Down Expand Up @@ -247,7 +248,7 @@ describe('run', () => {

expect(debugSpy).toHaveBeenCalledWith(
expect.stringMatching(
/\[\{"eventType":"TokensReplaced","application":"replacetokens-action","version":"1\.\d+\.\d+","account":"c054bf9f6127dc352a184a29403ac9114f6c2a8e27cb467197cdfc1c3df119e4","pipeline":"59830ebc3a4184110566bf1a290d08473dfdcbd492ce498b14cd1a5e2fa2e441","host":"server","os":"Windows","sources":3,"add-bom":false,"case-insensitive-paths":false,"chars-to-escape":"","encoding":"auto","escape":"auto","escape-char":"","if-no-files-found":"ignore","log-level":"info","missing-var-action":"none","missing-var-default":"","missing-var-log":"warn","recusrive":false,"separator":"\.","token-pattern":"default","token-prefix":"","token-suffix":"","transforms":false,"transforms-prefix":"\(","transforms-suffix":"\)","variable-files":0,"variable-envs":0,"inline-variables":0,"output-defaults":1,"output-files":2,"output-replaced":3,"output-tokens":4,"output-transforms":5,"result":"success","duration":\d+(?:\.\d+)?}]/
/\[\{"eventType":"TokensReplaced","application":"replacetokens-action","version":"1\.\d+\.\d+","account":"c054bf9f6127dc352a184a29403ac9114f6c2a8e27cb467197cdfc1c3df119e4","pipeline":"59830ebc3a4184110566bf1a290d08473dfdcbd492ce498b14cd1a5e2fa2e441","host":"server","os":"Windows","sources":3,"add-bom":false,"case-insensitive-paths":false,"chars-to-escape":"","encoding":"auto","escape":"auto","escape-char":"","if-no-files-found":"ignore","include-dot-paths":false,"log-level":"info","missing-var-action":"none","missing-var-default":"","missing-var-log":"warn","recusrive":false,"separator":"\.","token-pattern":"default","token-prefix":"","token-suffix":"","transforms":false,"transforms-prefix":"\(","transforms-suffix":"\)","variable-files":0,"variable-envs":0,"inline-variables":0,"output-defaults":1,"output-files":2,"output-replaced":3,"output-tokens":4,"output-transforms":5,"result":"success","duration":\d+(?:\.\d+)?}]/
)
);
});
Expand Down Expand Up @@ -324,7 +325,8 @@ describe('run', () => {
separator: rt.Defaults.Separator,
normalizeWin32: true,
root: '',
caseInsensitive: false
caseInsensitive: false,
dot: false
});

expect(replaceTokenSpy).toHaveBeenCalledWith(expect.anything(), expect.any(Function), expect.anything());
Expand Down Expand Up @@ -352,7 +354,8 @@ describe('run', () => {
separator: rt.Defaults.Separator,
normalizeWin32: true,
root: '',
caseInsensitive: false
caseInsensitive: false,
dot: false
});

expect(replaceTokenSpy).toHaveBeenCalledWith(expect.anything(), expect.any(Function), expect.anything());
Expand Down Expand Up @@ -380,7 +383,8 @@ describe('run', () => {
separator: rt.Defaults.Separator,
normalizeWin32: true,
root: '',
caseInsensitive: false
caseInsensitive: false,
dot: false
});

expect(replaceTokenSpy).toHaveBeenCalledWith(expect.anything(), expect.any(Function), expect.anything());
Expand Down Expand Up @@ -414,7 +418,8 @@ describe('run', () => {
separator: rt.Defaults.Separator,
normalizeWin32: true,
root: '',
caseInsensitive: false
caseInsensitive: false,
dot: false
});

expect(replaceTokenSpy).toHaveBeenCalledWith(expect.anything(), expect.any(Function), expect.anything());
Expand Down Expand Up @@ -452,7 +457,8 @@ describe('run', () => {
separator: rt.Defaults.Separator,
normalizeWin32: true,
root: '',
caseInsensitive: false
caseInsensitive: false,
dot: false
}
);

Expand Down Expand Up @@ -486,7 +492,8 @@ describe('run', () => {
separator: rt.Defaults.Separator,
normalizeWin32: true,
root: '',
caseInsensitive: false
caseInsensitive: false,
dot: false
});

expect(replaceTokenSpy).toHaveBeenCalledWith(expect.anything(), expect.any(Function), expect.anything());
Expand Down Expand Up @@ -650,6 +657,41 @@ describe('run', () => {
);
});

it('include-dot-paths', async () => {
// arrange
getBooleanInputSpy.mockImplementation(name => {
switch (name) {
case 'include-dot-paths':
return true;
default:
return false;
}
});

getInputSpy.mockImplementation(name => {
switch (name) {
case 'variables':
return '{}';
default:
return '';
}
});

// act
await run();

// assert
expect(setFailedSpy).not.toHaveBeenCalled();

expect(loadVariablesSpy).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ dot: true }));

expect(replaceTokenSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(Function),
expect.objectContaining({ sources: expect.objectContaining({ dot: true }) })
);
});

it('log-level: debug', async () => {
// arrange
getInputSpy.mockImplementation(name => {
Expand Down Expand Up @@ -993,12 +1035,7 @@ describe('run', () => {
// assert
expect(setFailedSpy).not.toHaveBeenCalled();

expect(loadVariablesSpy).toHaveBeenCalledWith(['{}'], {
separator: ':',
normalizeWin32: true,
root: '',
caseInsensitive: false
});
expect(loadVariablesSpy).toHaveBeenCalledWith(['{}'], expect.objectContaining({ separator: ':' }));

expect(replaceTokenSpy).toHaveBeenCalledWith(expect.anything(), expect.any(Function), expect.anything());
});
Expand Down

0 comments on commit 06848e0

Please sign in to comment.