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

Replace a block with a template between 'after' and 'before' or 'line_at' #436

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ You can guard against double injection:

* `skip_if` - a regular expression / text. If exists injection is skipped.

You can replace all the contents (a block) between different positions using:

* `after` and `before`, or `line_at` and `after` or `before`

Also you can insert or remove empty line to injection body. That feature very useful if your editor or formatter automatically insert blank line at the end of file on save:

* `eof_last` - if falsy - trim blank line from the end of injection body, if truthy - insert it.
Expand Down
12 changes: 6 additions & 6 deletions dist/ops/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ const locations = {
before: (_, lines) => getPragmaticIndex(_, lines, true),
after: (_, lines) => getPragmaticIndex(_, lines, false),
};
const indexByLocation = (attributes, lines) => {
const pair = Object.entries(attributes).find(([k, _]) => locations[k]);
if (pair) {
const indexesByLocation = (attributes, lines) => {
const pairs = Object.entries(attributes).filter(([k, _]) => locations[k]);
pairs.forEach((pair, i) => {
const [k, v] = pair;
return locations[k](v, lines);
}
return -1;
pairs[i] = locations[k](v, lines);
})
return pairs;
};
const injector = (action, content) => {
const { attributes: { skip_if, eof_last }, attributes, body, } = action;
Expand Down
4 changes: 4 additions & 0 deletions hygen.io/docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ Here are the available properties for an `inject: true` template:
* `prepend` or `append`, when true, add a line to start or end of file respectively.
* `at_line` which contains a line number will add a line at this exact line number.

You can replace all the contents (a block) between different positions using:

* `after` and `before`, or `at_line` and `after` or `before`

In almost all cases you want to ensure you're not injecting content twice:

* `skip_if` which contains a regular expression / text. If exists, injection is skipped.
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/__snapshots__/injector.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ exports[`injector before rails 1`] = `
"
`;

exports[`injector between rails 1`] = `
"
source 'http://rubygems.org'
gem 'rails'
gem 'kamikaze' # added by hygen
gem 'httparty'

"
`;

exports[`injector correctly interpret multi-line after regex 1`] = `
"
source 'http://rubygems.org'
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/injector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ describe('injector', () => {
),
).toMatchSnapshot()
})
it('between rails', () => {
expect(
injector(
{
attributes: {
after: "gem 'rails'",
before: "gem 'httparty'",
},
body: " gem 'kamikaze' # added by hygen",
},
gemfile,
),
).toMatchSnapshot()
})
it('prepend top of file', () => {
expect(
injector(
Expand Down
39 changes: 28 additions & 11 deletions src/ops/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ const locations = {
before: (_, lines) => getPragmaticIndex(_, lines, true),
after: (_, lines) => getPragmaticIndex(_, lines, false),
}
const indexByLocation = (attributes: any, lines: string[]): number => {
const pair = Object.entries(attributes).find(([k, _]) => locations[k])
if (pair) {
const [k, v] = pair
return locations[k](v, lines)
const indexesByLocation = (attributes: any, lines: string[]): number[] => {
const pairs = Object.entries(attributes).filter(([k, _]) => locations[k]);
if (pairs.length) {
const indexes: number[] = []
pairs.forEach((pair, i) => {
const [k, v] = pair;
indexes[i] = locations[k](v, lines);
})
return indexes;
}
return -1
}
};
const injector = (action: RenderedAction, content: string): string => {
const {
attributes: { skip_if, eof_last },
Expand All @@ -60,17 +63,31 @@ const injector = (action: RenderedAction, content: string): string => {
const lines = content.split(NL)

// returns -1 (end) if no attrs
const idx = indexByLocation(attributes, lines)
const idxs = indexesByLocation(attributes, lines)
let idx = -1
let deleteCount = 0
if (idxs.length > 2) {
throw new Error(`Too many injection attributes match '${attributes.join(',')}'! Don't know what to do.`)
}
if (idxs.length == 2) {
idxs.sort((a, b) => a - b) // numeric ascending
idx = idxs[0]
deleteCount = idxs[1] - idxs[0]
}
if (idxs.length == 1) {
idx = idxs[0]
deleteCount = 0
}

const trimEOF = idx >= 0 && eof_last === false && /\r?\n$/.test(body)
const insertEOF = idx >= 0 && eof_last === true && !/\r?\n$/.test(body)

if (trimEOF) {
lines.splice(idx, 0, body.replace(/\r?\n$/, ''))
lines.splice(idx, deleteCount, body.replace(/\r?\n$/, ''))
} else if (insertEOF) {
lines.splice(idx, 0, `${body}${NL}`)
lines.splice(idx, deleteCount, `${body}${NL}`)
} else if (idx >= 0) {
lines.splice(idx, 0, body)
lines.splice(idx, deleteCount, body)
}
return lines.join(NL)
} else {
Expand Down