match-file-contents
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

match-file-contents Build Status codecov

「 match file contents and rechange 」

Install

npm install match-file-contents
yarn add match-file-contents

Usage

var updateSection = require('match-file-contents');
 
var original = [
    '# Some Project'
  , ''
  , 'Does a bunch of things'
  , ''
  , 'START -- GENERATED GOODNESS'
  , 'this was painstakingly generated'
  , 'as was this'
  , 'END -- GENERATED GOODNESS' , ''
  , ''
  , '## The End'
  , ''
  , 'Til next time'
].join('\n');
 
let updateDate = original
 
var update = [
    'START -- GENERATED GOODNESS'
  , 'this was painstakingly re-generated'
  , 'and we added another line'
  , 'here'
  , 'END -- GENERATED GOODNESS'
].join('\n');
 
function matchesStart(line) {
  return (/START -- GENERATED GOODNESS/).test(line);
}
 
function matchesEnd(line) {
  return (/END -- GENERATED GOODNESS/).test(line);
}
 
let tags = updateSection.parse(original.split('\n', matchesStart, matchesEnd, true)
// Array - tags.length = 1
for(let i = 0; i < tags.length; i ++){
    // note the update data is from 0 => tags.length, one run one change ,
    // next time rePut updateDate to  updateSection(updateDate...
    updateDate = updateSection(updateDate, update, matchesStart, matchesEnd, i );
    console.log(updateDate);
}

Output

# Some Project

Does a bunch of things

START -- GENERATED GOODNESS
this was painstakingly re-generated
and we added another line
here
END -- GENERATED GOODNESS

## The End

Til next time

API

just like update-section#API

but

  • updateSection(updateDate, update, matchesStart, matchesEnd, index)

index = { 0 ~ tags.length}

  • updateSection.parse(original.split('\n', matchesStart, matchesEnd, keep)

keep if true, when tags.length === 1, can store the result tags, use by updateSection

concat

  • update-section Updates a section inside a file with newer content while removing the old content.

the lib is single section rechange , but match-file-contents can more

Use by

License

MIT © chinanf-boy

Readme

Keywords

Package Sidebar

Install

npm i match-file-contents

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

6.32 kB

Total Files

5

Last publish

Collaborators

  • yobrave1995