Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file and console libraries #858

Merged
merged 22 commits into from Jun 28, 2017
Merged

Add file and console libraries #858

merged 22 commits into from Jun 28, 2017

Conversation

@cmontella
Copy link
Contributor

cmontella commented Jun 26, 2017

No description provided.

cmontella added 2 commits Jun 26, 2017
…s' into feature/file-watcher
);
me.inputEAVs(changes);
} else {
console.log(err);

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 26, 2017

Contributor

This should probably drop in an error message of some sort. We haven't fleshed out what the story for #error is yet, but it'd be perfectly fine to have readfile.error += [code: <err code>, name:, text] for the program to look for and handle.

})

// Console log watcher
me.watch("print to console log", ({find, record}) => {

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 26, 2017

Contributor

As you mentioned, let's split this guy out.

});
})
})

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 26, 2017

Contributor

let's add a #file/write too? Then I can use this with the websocket watcher for the server backing the editor.

@joshuafcole joshuafcole changed the base branch from fix/run-eve-headless to master Jun 27, 2017
import {Watcher} from "./watcher";
import {ID} from "../runtime/runtime";

class consoleWatcher extends Watcher {

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

UpperCamel Case ConsoleWatcher

if(console) {
let {program:me} = this;

me.watch("print to console log", ({find, record}) => {

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

Let's format these as sentences for consistency. E.g. "Print to console."

record({log, text: log.text})
]
})
me.asObjects<{log:ID, text:string}>(({adds, removes}) => {

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

asDiffs is a fair bit faster than asObjects. For cases where you only need an id and a single attribute, I recommend using asDiffs instead. E.g.

me
  .watch("print to console log", ({find, record}) => {
    let log = find("console/log");
    return [log.add("text", log.text)];
  })
  .asDiffs(({adds}) => {
    for(let [log, _, text] of adds) {
      console.log(text);
    }
  })
}
}

Watcher.register("console", consoleWatcher);

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

Missing newline at end of file.

me.watch("read a file", ({find, record}) => {
let file = find("file/read");
return [
record({file, path: file.path, encoding: file.encoding})

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

I was going to suggest this. :) Let's give it utf-8 as a default though, e.g.

let encoding = choose(() => file.encoding, () => "utf-8");
me.asObjects<{file:ID, path:string, encoding:string}>(({adds, removes}) => {
Object.keys(adds).forEach((id) => {
let {file, path, encoding} = adds[id];
fs.readFile(path, {encoding: encoding}, function(err, contents){

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

{encoding: encoding} => {encoding}

changes.push(
[file, "contents", contents, 1],
);
me.inputEAVs(changes);

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

This whole branch can be reduced to me.inputEAVs([[file, "contents", contents]]). The count is optional and defaults to 1.

me.inputEAVs(changes);
} else {
let id = `${file}|error`
let changes:RawEAVC[] = [];

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

If you type this as RawEAV you can omit the counts.

let {file, path, contents, encoding} = adds[id];
fs.writeFile(path, contents, {encoding: encoding}, function(err){
if (!err) {
console.log(`Write file success: ${contents}`)

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

Instead of printing, let's set status: "complete" on the file record or something similar.

})
.asDiffs(({adds}) => {
for(let [log, _, text] of adds) {
console.log(text);

This comment has been minimized.

Copy link
@joshuafcole

joshuafcole Jun 27, 2017

Contributor

Seems like these for loops have all lost their indentation.

cmontella added 9 commits Jun 27, 2017
@cmontella cmontella changed the title Feature file watcher Add file and console libraries Jun 28, 2017
@joshuafcole joshuafcole merged commit 3c4e3e0 into master Jun 28, 2017
1 check passed
1 check passed
ci/circleci Your tests passed on CircleCI!
Details
@cmontella cmontella deleted the feature/file-watcher branch Jun 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.