Reuse spinner in sync methods without repeating code - node.js

In our code, we repeat the same sequence multiple times: starting a spinner, then execute a spawnSync method and update the spinner depending on result. For example here is one of the method:
cloneSync() {
const spinner = ora({
text: 'Cloning repository',
color: 'cyan',
spinner: 'arrow3'
}).start();
let clone = spawnSync('git', ['clone', repository.url, repository.name]);
if (clone.stderr) {
spinner.fail('Error while cloning repository');
throw new Error(clone.stderr);
} else {
spinner.succeed('Successfully cloned repository');
return clone.stdout;
}
}
Another code example so you can see the logic is almost identical:
parseLatestTagAndTransmitToDocker() {
const spinner = ora({
text: 'Checking latest tag',
color: 'cyan',
spinner: 'arrow3'
}).start();
let tag = spawnSync('git', ['describe', '--abbrev=0']);
if (tag.stderr) {
spinner.fail('Error while fetching latest tag of repository');
throw new Error(tag.stderr);
} else {
spinner.text(`Successfully retrieved latest tag: ${tag.stdout}`);
let docker = spawnSync('docker', ['run', 'myimage:latest', tag.stdout]);
if (docker.stderr) {
spinner.fail('Error while transmitting tag to docker image');
throw new Error(docker.stderr)
} else {
spinner.success('Successfully transmitted tag to docker service');
return docker.stdout;
}
}
}
Is it possible, in node 8+ to wrap this code and make it more reusable. I struggle finding a reusable code without having to trigger spinner and the if/else condition. Doing so with async allow use of try/catch and await/async. But here with sync method, I don't find the proper way to code that kind of behavior.

From the two examples you've provided, I can see a "SpinnerSpawner" function that returns a promise:
function spinnerSpawner(spinnerConfig, cmd, args) {
if (typeof(spinnerConfig) == "string") spinnerConfig = {
text: spinnerConfig,
color: "cyan",
spinner: "arrow3"
}
return new Promise(function(resolve, reject) {
let spinner = ora(spinnerConfig).start,
tag = spawnSync(cmd, args)
if (!tag.stdError) {
resolve(spinner, tag)
} else {
reject(spinner, tag)
}
})
}
cloneSync() {
spinnerSpawner("cloning repository", "git", ["clone", repository.url, repository.name])
.then(function(spinner, proc) {
spinner.succeed('Successfully cloned repository');
return proc.stdout;
}, function(spinner, proc) {
spinner.fail('Error while cloning repository');
throw new Error(proc.stderr);
}
)
}
parseLatestTagAndTransmitToDocker() {
spinnerSpawner("Checking latest tag", "git", ["describe", "--abbrev=0"])
.then(
function(spinner, proc) {
spinner.text(`successfully retrieved latest tag: ${proc.stdout}`)
return spinnerSpawner("checking docker", "docker", ["run", "myimage:latest", proc.stdout])
}
).then(
function(spinner, proc) {
spinner.success("Processing completed")
return proc.stdout
},
function(spinner, proc) {
spinner.fail(`processing error: ${proc.stderr}`);
throw new Error(tag.stderr);
}
)
}
as always, my code is pseudo-code and not fit for execution - let alone production!

Related

Core Data: how to have a default value

self learning beginner here.
Although in the xcdatamodel file I have the default value for the attribute of that entity (see screenshot), when I build the preview, the Text("+") isn't there. I guess the countnum attribute in the TargetEntity is still empty. My thinking is to have the add() run if the attribute is empty. But that doesn't work either. Is there a way to automatically initialize the attribute when the app runs, instead of needing to build a button for the user to press?
Thanks a million
struct ListView: View {
#Environment(\.managedObjectContext) var moc
#FetchRequest(sortDescriptors: []) var targets: FetchedResults<TargetEntity>
var body: some View {
VStack {
if let firstItem = targets.first {
Text("+")
.onTapGesture (count: 2){
do {
increment(firstItem)
try moc.save()
} catch {
print(error)
add()
}
}
}
}
}
func increment(_ item: TargetEntity) {
item.countnum += 1
save()
}
func add() {
let countnum = TargetEntity(context: moc)
countnum.countnum = 0
save()
}
func save() {
do { try moc.save() } catch { print(error) }
}
}

How to create blueprint for needle-client-react in Jhipster?

I'm using generator-jhipster and I want to create blueprints for entity-client. After writing entity files, a postWriting function will call addEnitiyToMenu in generator-jhipster/generators/client/needle-api/needle-client-react.js to add new entity generated to file menu/entities.tsx
I need to override this function to write a different entityEntry with original one.
But I can't find the template for it. What should I do?
I found that I can write these function by my own. There is example code if you need
function generateFileModel(aFile, needleTag, ...content) {
return {
file: aFile,
needle: needleTag,
splicable: content,
};
}
function addBlockContentToFile(rewriteFileModel, generator) {
try {
return jhipsterUtils.rewriteFile(rewriteFileModel, generator);
} catch (e) {
console.error(e);
return null;
}
}
function addToMenu() {
if (this.skipClient) return;
if (!this.embedded) {
this.addEntityToModule();
const entityMenuPath = `${this.CLIENT_MAIN_SRC_DIR}app/shared/layout/menus/entities.tsx`;
const entityEntry =
// prettier-ignore
this.stripMargin(`|<Menu.Item key="${this.entityStateName}" icon={<FileOutlined />}>
| <Link to="/${this.entityStateName}">
| ${this.enableTranslation ? `<Translate contentKey="global.menu.entities.${this.entityTranslationKeyMenu}" />` : `${_.startCase(this.entityStateName)}`}
| </Link>
| </Menu.Item>`);
const rewriteFileModel = generateFileModel(entityMenuPath, 'jhipster-needle-add-entity-to-menu', entityEntry);
addBlockContentToFile(rewriteFileModel, this);
}
}
function replaceTranslations() {
if (this.clientFramework === VUE && !this.enableTranslation) {
if (!this.readOnly) {
utils.vueReplaceTranslation(this, [
`app/entities/${this.entityFolderName}/${this.entityFileName}.vue`,
`app/entities/${this.entityFolderName}/${this.entityFileName}-update.vue`,
`app/entities/${this.entityFolderName}/${this.entityFileName}-details.vue`,
]);
} else {
utils.vueReplaceTranslation(this, [
`app/entities/${this.entityFolderName}/${this.entityFileName}.vue`,
`app/entities/${this.entityFolderName}/${this.entityFileName}-details.vue`,
]);
}
}
}

how to implement string list from _ListItem to assetPath?

i've been working with wallpaper app.
i manage to call the path list for wallpaper,
i can make this work on image card list but i can't manage to do it work with assetPath for WallpaperManager
Please help, is there any way to do it?
Future<void> setWallpaperFromAsset() async {
setState(() {
_wallpaperAsset = "Loading";
});
String result;
String assetPath = ('Load List here');
You can add a parameter assetPath to setWallpaperFromAsset
Future<void> setWallpaperFromAsset(String assetPath) async {
setState(() {
_wallpaperAsset = "Loading";
});
String result;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
result = await WallpaperManager.setWallpaperFromAsset(
assetPath, WallpaperManager.HOME_SCREEN);
} on PlatformException {
result = 'Failed to get wallpaper.';
}
}
And you register the onPressed callback with:
RaisedButton.icon(
onPressed: () => setWallpaperFromAsset(item),
icon: Icon (Icons.wallpaper, size: 20),
label: Text('homescreen')),
)

Making an asynchronous function synchronous for the Node.js REPL

I have a library that connects to a remote API:
class Client(access_token) {
void put(key, value, callback);
void get(key, callback);
}
I want to set up a Node.js REPL to make it easy to try things out:
var repl = require('repl');
var r = repl.start('> ');
r.context.client = new Client(...);
The problem is that an asynchronous API is not convenient for a REPL. I'd prefer a synchronous one that yields the result via the return value and signals an error with an exception. Something like:
class ReplClient(access_token) {
void put(key, value); // throws NetworkError
string get(key); // throws NetworkError
}
Is there a way to implement ReplClient using Client? I'd prefer to avoid any dependencies other than the standard Node.js packages.
You can synchronously wait for stuff with the magic of wait-for-stuff.
Based on your example specification:
const wait = require('wait-for-stuff')
class ReplClient {
constructor(access_token) {
this.client = new Client(access_token)
}
put(key, value) {
return checkErr(wait.for.promise(this.client.put(key, value)))
}
get(key) {
return checkErr(wait.for.promise(this.client.get(key)))
}
}
const checkErr = (maybeErr) => {
if (maybeErr instanceof Error) {
throw maybeErr
} else {
return maybeErr
}
}

Reduce code repetition in Groovy closures

In a piece of Gradle build script, the amount of code i'm repeating is increasing. All tasks have a big part in common, except for a few lines:
task copyZipFile() {
doLast {
def remoteBuildProperties = getRemoteBuildProperties(project)
ant {
taskdef(name: 'ftp',
classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP',
classpath: configurations.ftpAntTask.asPath)
ftp(server: remoteBuildProperties['host.name'],
userid: remoteBuildProperties['username'],
password: remoteBuildProperties['password'],
remotedir: 'some-folder', // This value differs from call to call
passive: 'true') {
// The next two lines also are different per case, and might be less or more lines
fileset(dir: rootProject.buildDir) { include(name: 'build.zip') }
fileset(dir: rootProject.projectDir) { include(name: 'build.properties') }
}
}
}
}
I don't like to repeat myself, so I'd like to reduce this code to a new helper method that does this trick, and a simple caller, something like:
task copyZipFile() {
doLast {
def remoteBuildProperties = getRemoteBuildProperties(project)
upload(remoteBuildProperties, 'some-folder') {
fileset(dir: rootProject.buildDir) { include(name: 'build.zip') }
fileset(dir: rootProject.projectDir) { include(name: 'build.properties') }
}
}
}
How would I achieve this?
You can pass the inner closure to your upload method as the final parameter. Set the delegate to the original builder delegate so the inner closure calls get handled properly. For example:
def upload(remoteBuildProperties, folder, body) {
ant {
taskdef(name: 'ftp',
classname: 'org.apache.tools.ant.taskdefs.optional.net.FTP',
classpath: configurations.ftpAntTask.asPath)
ftp(server: remoteBuildProperties['host.name'],
userid: remoteBuildProperties['username'],
password: remoteBuildProperties['password'],
remotedir: folder,
passive: 'true') {
body.delegate = delegate
body()
}
}
}

Resources