diff --git a/README.md b/README.md index 3af0a3c..271646d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,18 @@ # indexer-github-action -Github action to automatically dump an index.json in every folder of a repository that lists all files inside it +Github action to automatically dump an index.json in every folder of a repository that lists all files inside it. + +- Also adds "tags" to every file in the index by extracting information in the filename. +- Tags must be supplied in the filename using the following format: `.key-value.` +- For example, if there's a file called `landingImage.language-en.png`, it will be parsed as follows: + +```json +{ + "name": "landingImage.language-en.png", + "type": "file", + "tags": { "language": "en" } +} +``` ### References diff --git a/src/index.js b/src/index.js index c0e5597..c7c9ea4 100644 --- a/src/index.js +++ b/src/index.js @@ -6,9 +6,21 @@ const configFileName = ".indexer.yaml" const getFileDetails = fileName => { const stats = fs.lstatSync(fileName) + const name = path.basename(fileName) + const parsedName = name && name.split(".") + let tags = {} + for (const potentialTag of parsedName) { + const parsedTag = potentialTag.split("-") + // If potential tag element contains exactly one hyphen, then it's a tag + if (parsedTag.length === 2) { + tags[parsedTag[0]] = parsedTag[1] + } + } + return { - name: path.basename(fileName), - type: stats.isDirectory() ? "folder" : "file" + name, + type: stats.isDirectory() ? "folder" : "file", + tags } } @@ -47,9 +59,14 @@ const dirTree = folderName => { } }) - fs.writeFile(`${folderName}/index.json`, JSON.stringify(index, null, 2), "utf8", () => { - console.log(`Index written for ${folderName}`) - }) + fs.writeFile( + `${folderName}/index.json`, + JSON.stringify(index, null, 2), + "utf8", + () => { + console.log(`Index written for ${folderName}`) + } + ) return index } diff --git a/test/indexer.spec.js b/test/indexer.spec.js index 3780c28..90bb972 100644 --- a/test/indexer.spec.js +++ b/test/indexer.spec.js @@ -42,9 +42,15 @@ describe("Indexer Tests", () => { it("Should ignore deprecated files and folders", done => { const a = JSON.parse(fs.readFileSync("test/testFolder/index.json")) - expect(a.length).to.equal(4) + expect(a.length).to.equal(5) const y = JSON.parse(fs.readFileSync("test/testFolder/y/index.json")) expect(y.length).to.equal(1) done() }) + + it("Should parse tags in filenames", done => { + const a = JSON.parse(fs.readFileSync("test/testFolder/index.json")) + expect(a[3].tags.tagKey).to.equal("tagValue") + done() + }) }) diff --git a/test/templateFolder/taggedFile.tagKey-tagValue.txt b/test/templateFolder/taggedFile.tagKey-tagValue.txt new file mode 100644 index 0000000..e69de29