Skip to content

Commit

Permalink
Add support for parsing tags inside filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjaypojo committed Jan 21, 2020
1 parent 96d30a4 commit c42132e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
27 changes: 22 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 7 additions & 1 deletion test/indexer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
Empty file.

0 comments on commit c42132e

Please sign in to comment.