Skip to content
Snippets Groups Projects
Commit 6dd53d1f authored by Rhys Arkins's avatar Rhys Arkins
Browse files

refactor(pub): use autoReplace

parent 42480aa4
No related branches found
No related tags found
No related merge requests found
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`manager/pub/update updateDependency replaces nested value 1`] = `
"dev_dependencies:
test: 0.1
build:
version: 0.1
dependencies:
bar:
sdk: flatter
foo: 1
bar:
version: 1.2.3
baz:
non-sense: true
qux: false
"
`;
exports[`manager/pub/update updateDependency replaces one-line value 1`] = `
"dev_dependencies:
test: 0.1
build:
version: 0.1
dependencies:
bar:
sdk: flatter
foo: 1.2.3
bar:
version: 1
baz:
non-sense: true
qux: false
"
`;
import * as npmVersioning from '../../versioning/npm';
export { extractPackageFile } from './extract';
export { updateDependency } from './update';
export const autoReplace = true;
export const defaultConfig = {
fileMatch: ['(^|/)pubspec\\.ya?ml$'],
......
import { readFileSync } from 'fs';
import { updateDependency } from '.';
const fileContent = readFileSync(
'lib/manager/pub/__fixtures__/update.yaml',
'utf8'
);
describe('manager/pub/update', () => {
describe('updateDependency', () => {
it('returns content untouched if versions are same', () => {
const upgrade = {
depName: 'foo',
currentValue: '1',
newValue: '1',
depType: 'dependencies',
};
const res = updateDependency({ fileContent, upgrade });
expect(res).toEqual(fileContent);
});
it('returns null if content was updated', () => {
expect(
updateDependency({
fileContent,
upgrade: {
depName: 'test',
currentValue: '0.0.1',
newValue: '1',
depType: 'dev_dependencies',
},
})
).toBe(null);
expect(
updateDependency({
fileContent,
upgrade: {
depName: 'build',
currentValue: '0.0.1',
newValue: '1',
depType: 'dev_dependencies',
},
})
).toBe(null);
});
it('replaces one-line value', () => {
const upgrade = {
depName: 'foo',
currentValue: '1',
newValue: '1.2.3',
depType: 'dependencies',
};
const res = updateDependency({ fileContent, upgrade });
expect(res).not.toEqual(fileContent);
expect(res.includes(upgrade.newValue)).toBe(true);
expect(res).toMatchSnapshot();
});
it('replaces nested value', () => {
const upgrade = {
depName: 'bar',
currentValue: '1',
newValue: '1.2.3',
depType: 'dependencies',
};
const res = updateDependency({ fileContent, upgrade });
expect(res).not.toEqual(fileContent);
expect(res.includes(upgrade.newValue)).toBe(true);
expect(res).toMatchSnapshot();
});
});
});
import { load } from 'js-yaml';
import { UpdateDependencyConfig } from '../common';
export function updateDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): string {
const { depName, depType, currentValue, newValue } = upgrade;
if (currentValue === newValue) {
return fileContent;
}
const sectionBeginRegExp = new RegExp(`^${depType}:`);
const isSectionBegin = (line: string): boolean =>
sectionBeginRegExp.test(line);
const isSectionEnd = (line: string): boolean => /^[^\s]/.test(line);
const simpleDepRegExp = new RegExp(`^\\s+${depName}:\\s*[^\\s]+\\s*$`);
const isOneLineDep = (line: string): boolean => simpleDepRegExp.test(line);
const multilineDepRegExp = new RegExp(`^\\s+${depName}:\\s*$`);
const isMultilineDepRegExp = (line: string): boolean =>
multilineDepRegExp.test(line);
const versionRegExp = new RegExp('^\\s+version:\\s*[^\\s]+\\s*$');
const isVersionLine = (line: string): boolean => versionRegExp.test(line);
const isValidVersion = (line: string): boolean => {
const version = load(line.replace(/^.*:\s*/, '')).toString();
return version === currentValue;
};
let isSection = false;
let indent = null;
const lines = fileContent.split('\n');
const len = lines.length;
for (let idx = 0; idx < len; idx += 1) {
const line = lines[idx];
if (isSectionBegin(line)) {
isSection = true;
} else if (isSectionEnd(line)) {
isSection = false;
} else if (isSection) {
if (isOneLineDep(line)) {
if (!isValidVersion(line)) {
return null;
}
lines[idx] = line.replace(currentValue, newValue);
break;
} else if (isMultilineDepRegExp(line)) {
indent = line.search(/[^\s]/);
} else if (indent) {
const currentIndent = line.search(/[^\s]/);
if (currentIndent <= indent) {
indent = null;
} else if (isVersionLine(line)) {
if (!isValidVersion(line)) {
return null;
}
lines[idx] = line.replace(currentValue, newValue);
break;
}
}
}
}
return lines.join('\n');
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment