Skip to content

Instantly share code, notes, and snippets.

@truongsinh
Created January 10, 2019 04:03
Show Gist options
  • Save truongsinh/a42d7ff9902cfd98b9bd900311ef11d5 to your computer and use it in GitHub Desktop.
Save truongsinh/a42d7ff9902cfd98b9bd900311ef11d5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const dest = __dirname + "/../packages/";
const { readdir, readFile, writeFile } = require("fs");
const { promisify } = require("util");
const [readDirAsync, writeFileAsync, readFileAsync] = [
promisify(readdir),
promisify(writeFile),
promisify(readFile)
];
const rp = require("request-promise-native");
const { JSDOM } = require("jsdom");
class Lazy {
constructor(iterable, callback) {
this.iterable = iterable;
this.callback = callback;
}
filter(callback) {
return new LazyFilter(this, callback);
}
map(callback) {
return new LazyMap(this, callback);
}
next() {
return this.iterable.next();
}
take(n) {
const values = [];
for (let i = 0; i < n; i++) {
values.push(this.next().value);
}
return values;
}
}
class LazyFilter extends Lazy {
next() {
while (true) {
const item = this.iterable.next();
if (this.callback(item.value)) {
return item;
}
}
}
}
class LazyMap extends Lazy {
next() {
const item = this.iterable.next();
const mappedValue = this.callback(item.value);
return { value: mappedValue, done: item.done };
}
}
const main = async () => {
const dirList = await readDirAsync(dest);
const dirListGenerator = new Lazy(
(function*() {
for (let i = 0; i < dirList.length; i++) {
yield dirList[i];
}
})()
);
dirList
// dirListGenerator
.map(async dirName => {
if (!dirName) {
return;
}
try {
// const packageAndroidContent = await readDirAsync(__dirname + "/../packages/" + dirName + "android/src");
const gradlePath =
__dirname + "/../packages/" + dirName + "/android/build.gradle";
const gradleContent = await readFileAsync(gradlePath);
const gradleContentString = gradleContent.toString();
const matches = gradleContentString.match(/\n\ndependencies \{/i);
if (matches) {
return;
}
const content =
gradleContentString +
`
dependencies {
testImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12'
}
`;
await writeFileAsync(gradlePath, content);
} catch (e) {
console.log(e);
}
});
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment