0.3.13 • Published 7 years ago

resettable-file v0.3.13

Weekly downloads
12
License
MIT
Repository
github
Last release
7 years ago

resettable-file

Resettable files for creating and maintaining boilerplates and configurations

Commitizen friendly

Description

Provides utility class and methods for boilerplate projects to create/copy/remove files, directories and data files (json/yaml). Created files, directories and data files are tracked and recorded to a json file, and modifications done by this library can be undone by reset() method.

Please note that, this module does not try to be a version conrol system. Main purpose of this module is to modify and reverse selected files only.

Synopsis

Written using TypeScript

const ResettableFile = require("resettable-file");

// Set "warn" (default) or "error" for less noise
const resettableFile = new ResettableFile("./registry.json", { logLevel: "info" });

// Track changes by key/value level
const packageData = resettableFile.getDataObjectSync("./package.1.json");

// Modify package.json
packageData.set("scripts.myscript", "some-cmd").set("scripts.ls", "ls -al");

// Write data to file
resettableFile.writeFileSync("some.txt", "Some text data here");

// Create a symbolic link
resettableFile.createSymLinkSync("/some/where/tsconfig.json", "tsconfig.json");

// Create directory tree
resettableFile.createDirSync("some/deep/path");

// Save changes to registry
resettableFile.saveSync();
// Delete created keys in package.json, delete file and symbolic link if not changed
// and delete directory tree if they are empty:
resettableFile.resetSync();

Reset

Can

  • Delete created files by this library if they are not changed outside.
  • Delete created symbolic links if they are not changed outside.
  • Delete created directories,
    • if they are empty
    • or all files in directories are created by this libray and they are not changed outside.
  • Reset key/value pairs in data files (modifictions made by this library)
    • Delete created keys,
    • Create old keys and values for deleted keys,
    • Write old values to replaced keys,
    • Delete created values from arrays
    • Insert deleted values in to arrays (closest index possible)

Can't

  • Create deleted files.
  • Create deleted directories.
  • Replace old content of files (except data files)
  • Recreate deleted directories
  • Recreate deleted symbolic links

As seen from what can and can't be done, ResettableFile is a utility to create and modify text based files and data files (json and yaml) and not a backup/version control for deleted files.

API

Classes

DataObject

Kind: global class

new DataObject(data, options)

ParamTypeDefaultDescription
dataObject{}Data to be modified.
optionsObjectOptions
options.trackbooleanWhether to track changes.
options.sortKeysArray.<string>List of keys which their values shoud be sorted. Key names be paths like "scripts.test"
options.namestringPath of the name to be used in logs.
options.formatFormatData format used for parsing and serializing data.
options.operationsArray.<Operation>Operations to reset data to its original state.
options.loggerLoggerA looger instance such as winston. Must implement silky, verbose, info, warn, error.

dataObject.isChanged : boolean

Kind: instance property of DataObject
Read only: true

dataObject.data : Data

Kind: instance property of DataObject
Read only: true

dataObject.original : Data

Kind: instance property of DataObject
Read only: true

dataObject.snapshot : Data

Kind: instance property of DataObject
Read only: true

dataObject.has(props, t, f) ⇒ *

Kind: instance method of DataObject
Returns: * -

ParamTypeDefaultDescription
propsstring | Array.<Path>Property or properties to look in data
t*trueValue to return if some of the properties exists in project.
f*falseValue to return if none of the properties exists in project.

Example

const result = project.hasProp(["scripts.build", "scripts.build:doc"], "yes", "no");

dataObject.hasSubProp(prop, subProps, t, f) ⇒ *

Kind: instance method of DataObject
Returns: * -

ParamTypeDefaultDescription
propPathProperty or properties to look in data
subPropsstring | Array.<Path>Property or properties to look in data
t*trueValue to return if some of the properties exists.
f*falseValue to return if none of the properties exists.

Example

const result = project.hasSubProp("scripts", ["build", "build:doc"]);
const result2 = project.hasSubProp("address.home", ["street.name", "street.no"]);

dataObject.get(path) ⇒ *

Kind: instance method of DataObject
Returns: * -

ParamTypeDescription
pathPathPath to get data from

dataObject.set(path, value, options) ⇒ this

Kind: instance method of DataObject
Returns: this -

ParamTypeDefaultDescription
pathPathPath to store data at.
value*Value to store at given key.
optionsObjectOptions
options.forcebooleanfalseWhether to force change even value is altered by user manually.

dataObject.setObject(data, options) ⇒ this

Kind: instance method of DataObject
Returns: this -

ParamTypeDefaultDescription
dataObjectData to store at object.
optionsObjectOptions
options.forcebooleanfalseWhether to force change even value is altered by user manually.

Example

data.setObject({ "a.b": 1, c: 2, d: 3 });

dataObject.remove(path, options) ⇒ this

Kind: instance method of DataObject
Returns: this -

ParamTypeDefaultDescription
pathstring | Array.<Path>Path or list of paths to remove
optionsObjectOptions
options.forcebooleanfalseWhether to force change even value is altered by user manually.

dataObject.reset() ⇒ Array.<Operation>

Kind: instance method of DataObject
Returns: Array.<Operation> -

ResettableFile

Kind: global class

new ResettableFile(registryFile, options)

ParamTypeDefaultDescription
registryFilestringPath of the registry file. Registry file's directory is also root directory.
optionsObjectOptions
options.sourceRootstringSource root. If provided all source files are calculated relative to this path for copy, symbolic link etc.
options.trackbooleanSets default tracking option for methods.
options.logLevelstring"\"warn\""Sets log level if default logger is used. ("error", "warn", "info", "debug", "verbose", "silly")
options.loggerBasicLoggerA looger instance such as winston. Must implement info, warn, error, verbose, silly.

resettableFile.root : string

Kind: instance property of ResettableFile
Read only: true

resettableFile.sourceRoot : string

Kind: instance property of ResettableFile
Read only: true

resettableFile.track : boolean

Kind: instance property of ResettableFile
Read only: true

resettableFile.logger : BasicLogger

Kind: instance property of ResettableFile
Read only: true

resettableFile.logLevel : string

Kind: instance property of ResettableFile

resettableFile.fromRoot(...part) ⇒ string

Kind: instance method of ResettableFile
Returns: string -

ParamTypeDescription
...partstringPath or path parts to get full path in root.

Example

const resettable = new ResettableFile({ registryFile: "dir/registry.json" });
resettable.fromRoot("path/to/file.txt"); // dir/path/to/file.txt

resettableFile.fromSourceRoot(...part) ⇒ string

Kind: instance method of ResettableFile
Returns: string -

ParamTypeDescription
...partstringPath or path parts to get full path in root.

Example

const resettable = new ResettableFile({ sourceRoot: "sourcedir" });
resettable.fromSourceRoot("path/to/file.txt"); // sourcedir/path/to/file.txt

resettableFile.isDataFile(projectFile) ⇒ boolean

Kind: instance method of ResettableFile
Returns: boolean -

ParamTypeDescription
projectFilestringFile to check

resettableFile.hasFileSync(projectFiles, t, f) ⇒ *

Kind: instance method of ResettableFile
Returns: * -

ParamTypeDefaultDescription
projectFilesstring | Array.<string>File or list of files to look in root.
t*trueValue to return if any of the files exists in root.
f*falseValue to return if none of the files exists in root.

resettableFile.isBrokenLink(projectFile) ⇒ boolena

Kind: instance method of ResettableFile
Returns: boolena -

ParamTypeDescription
projectFilestringProject file to check.

resettableFile.saveSync() ⇒ void

Kind: instance method of ResettableFile
Throws:

  • VError

resettableFile.resetSync() ⇒ void

Kind: instance method of ResettableFile
Throws:

  • VError

resettableFile.resetFileSync(projectFile) ⇒ void

Kind: instance method of ResettableFile
Throws:

  • VError
ParamTypeDescription
projectFilestringFile to reset

resettableFile.getFileDetailsSync(projectFile, options) ⇒ FileDetail

Kind: instance method of ResettableFile
Returns: FileDetail -

  • VError
ParamTypeDescription
projectFilestringFile to get detail for.
optionsObjectOptions
options.forcebooleanAssume safe to operate on file even it is not auto created.
options.trackbooleanWhether to track file if it is created by module.

resettableFile.getFileHashSync(projectFile) ⇒ string

Kind: instance method of ResettableFile
Returns: string -

ParamTypeDescription
projectFilestringFile path of the file relative to root to calculate hash for.

resettableFile.getDataObjectSync(projectFile, options) ⇒ DataObject

Kind: instance method of ResettableFile
Returns: DataObject -

  • VError
ParamTypeDefaultDescription
projectFilestringFile path to read relative to root.
optionsObjectOptions
options.createbooleanfalseWhether to create file if it does not exist.
options.defaultContentObjectDefault content to write if file is created.
options.throwNotExistsbooleantrueThrow error if file does not exist.
options.formatbooleanfile extensionFormat to serialize data in given format. (json or yaml) Default is json.
options.createFormatboolean"json"Format to serialize data in given format. (json or yaml) Default is json.
options.trackbooleanthis.trackWhether to track file in registry if it is created by module.
options.forcebooleanfalseWhether to force write file even it exist.
options.sortKeysArray.<string>List of keys which their values shoud be sorted. Key names be paths like "scripts.test"

resettableFile.createSymLinkSync(targetFile, projectFile, options) ⇒ void

Kind: instance method of ResettableFile
Throws:

  • VError
ParamTypeDefaultDescription
targetFilestringTarget file which link points to. Should be given relative to module root.
projectFilestringLink file. Should be given relative to project root.
optionsObjectOptions
options.forcebooleanfalseWrites file even it exists and not auto created.
options.trackbooleanthis.trackWhether to track symlink if it is created by module.

Example

// Creates tsconfig.json symbolic link file in project root, pointing to a file from toolkit.
createSymLink(here("../../config.json"), "tsconfig.json");

resettableFile.readFileSync(projectFile, options) ⇒ *

Kind: instance method of ResettableFile
Returns: * -

  • VError
ParamTypeDefaultDescription
projectFilestringFile to read relative to root.
optionsObjectOptions
options.createbooleanfalseWhether to create file if it does not exist.
options.trackbooleanthis.trackWhether to track file in registry if it is created by module.
options.forcebooleanfalseWhether to force create even it is deleted by user.
options.defaultContent*Default content to write if file does not exist.
options.throwNotExistsbooleantrueThrow error if file does not exist.
options.parsebooleanfalseWhether to parse file content to create a js object.
options.formatFormatfile extensionFormat to use parsing data.
options.createFormatFormatjsonFormat to be used while creating nonexisting file if no format is provided.
options.serializeFormatparseWhether to serialize content if file is created. (Default is status of parse option)

resettableFile.readFileDetailedSync(projectFile, options) ⇒ Object

Kind: instance method of ResettableFile
Returns: Object -

  • VError
ParamTypeDefaultDescription
projectFilestringFile to read relative to project root.
optionsObjectOptions
options.createbooleanfalseWhether to create file if it does not exist.
options.trackbooleanthis.trackWhether to track file in registry if it is created by module.
options.forcebooleanfalseWhether to force create even it is deleted by user.
options.defaultContent*Default content to write if file does not exist.
options.throwNotExistsbooleantrueThrow error if file does not exist.
options.parsebooleanfalseWhether to parse file content to create a js object.
options.formatFormatfile extensionFormat to use parsing data.
options.createFormatFormatjsonFormat to be used while creating nonexisting file if no format is provided.
options.serializeFormatparseWhether to serialize content if file is created. (Default is status of parse option)

resettableFile.writeFileSync(projectFile, data, options) ⇒ void

Kind: instance method of ResettableFile
Throws:

  • VError
ParamTypeDefaultDescription
projectFilestringFile path to relative to project root.
datastringData to write
optionsObjectOptions
options.forcebooleanfalseWrites file even it exists and not auto created.
options.trackbooleanthis.trackWhether to track file in registry if it is created by module.
options.serializebooleanfalseWhether to serialize object before written to file.
options.formatFormatfile extensionFormat to use serializing data.
options.sortKeysArrayKeys to be sorted. Keys may be given as chained paths. (i.e. a.b.c -> Keys of c would be sorted)

Example

project.writeFile("./some-config.json", '{"name": "my-project"}'); // Writes given data to some-config.json in project root.

resettableFile.deleteFileSync(projectFile, options) ⇒ void

Kind: instance method of ResettableFile
Throws:

  • VError
ParamTypeDefaultDescription
projectFilestringFile path relative to paroject root.
optionsObjectOptions
options.forcebooleanfalseDeletes file even it exists and not auto created.
options.trackbooleanthis.trackWhether to operate in tracked mode. In non-tracked mode existing files are not deleted if force is not active.
options.logbooleantrueWhether to log operation.
options.brokenLinkbooleanfalseWhether project file is a broken link. Broken links are treated as if they do not exist.

Example

project.copy("./some-config.json", "./some-config.json"); // Copies some-config.json file from module's root to project's root.

resettableFile.copyFileSync(sourceFile, projectFile, options) ⇒ void

Kind: instance method of ResettableFile
Throws:

  • VError
ParamTypeDefaultDescription
sourceFilestringSource file path.
projectFilestringDestinantion file path relative to paroject root.
optionsObjectOptions
options.forcebooleanfalseOverwrites file even it exists and not auto created.
options.trackbooleanthis.trackWhether to track file in registry if it is created by module.

Example

project.copy("./some-config.json", "./some-config.json"); // Copies some-config.json file from module's root to project's root.

resettableFile.createDirSync(projectDir, options) ⇒ void

Kind: instance method of ResettableFile
Throws:

  • VError
ParamTypeDefaultDescription
projectDirstringDirectory path to relative to project root.
optionsObjectOptions
options.trackbooleanthis.trackWhether to track file in registry if it is created by module.
options.logDirsbooleantrueWhether to log delete operation of sub directories.

Example

project.createDir("path/to/dir"); // Created "path", "to" and "dir" as necessary.

resettableFile.deleteDirSync(projectDir, options) ⇒ void

Kind: instance method of ResettableFile
Throws:

  • VError
ParamTypeDefaultDescription
projectDirstringDestinantion directory to delete.
optionsObjectOptions
options.forcebooleanfalseDeletes directory and it's contents even it is not empty.
options.trackbooleanthis.trackWhether to track file in registry if it is created by module.
options.logFilesbooleantrueWhether to log delete operation of files.
options.logDirsbooleantrueWhether to log delete operation of sub directories.
0.3.13

7 years ago

0.3.12

7 years ago

0.3.11

7 years ago

0.3.10

7 years ago

0.3.9

7 years ago

0.3.8

7 years ago

0.3.6

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.0.0

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago