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

Conversation

Projects
None yet
2 participants
@cmontella
Contributor

cmontella commented Jun 26, 2017

No description provided.

Show outdated Hide outdated src/watchers/file.ts
);
me.inputEAVs(changes);
} else {
console.log(err);

This comment has been minimized.

@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.

@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.

Show outdated Hide outdated src/watchers/file.ts
})
// Console log watcher
me.watch("print to console log", ({find, record}) => {

This comment has been minimized.

@joshuafcole

joshuafcole Jun 26, 2017

Contributor

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

@joshuafcole

joshuafcole Jun 26, 2017

Contributor

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

Show outdated Hide outdated src/watchers/file.ts
});
})
})

This comment has been minimized.

@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 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

Show outdated Hide outdated src/watchers/console.ts
import {Watcher} from "./watcher";
import {ID} from "../runtime/runtime";
class consoleWatcher extends Watcher {

This comment has been minimized.

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

UpperCamel Case ConsoleWatcher

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

UpperCamel Case ConsoleWatcher

Show outdated Hide outdated src/watchers/console.ts
if(console) {
let {program:me} = this;
me.watch("print to console log", ({find, record}) => {

This comment has been minimized.

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

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

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

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

Show outdated Hide outdated src/watchers/console.ts
record({log, text: log.text})
]
})
me.asObjects<{log:ID, text:string}>(({adds, removes}) => {

This comment has been minimized.

@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);
    }
  })
@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);
    }
  })
Show outdated Hide outdated src/watchers/console.ts
}
}
Watcher.register("console", consoleWatcher);

This comment has been minimized.

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

Missing newline at end of file.

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

Missing newline at end of file.

Show outdated Hide outdated src/watchers/file.ts
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.

@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");
@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");
Show outdated Hide outdated src/watchers/file.ts
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.

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

{encoding: encoding} => {encoding}

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

{encoding: encoding} => {encoding}

Show outdated Hide outdated src/watchers/file.ts
changes.push(
[file, "contents", contents, 1],
);
me.inputEAVs(changes);

This comment has been minimized.

@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.

@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.

Show outdated Hide outdated src/watchers/file.ts
me.inputEAVs(changes);
} else {
let id = `${file}|error`
let changes:RawEAVC[] = [];

This comment has been minimized.

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

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

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

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

Show outdated Hide outdated src/watchers/file.ts
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.

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

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

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

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

Show outdated Hide outdated src/watchers/console.ts
})
.asDiffs(({adds}) => {
for(let [log, _, text] of adds) {
console.log(text);

This comment has been minimized.

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

Seems like these for loops have all lost their indentation.

@joshuafcole

joshuafcole Jun 27, 2017

Contributor

Seems like these for loops have all lost their indentation.

@cmontella cmontella changed the title from Feature file watcher to Add file and console libraries Jun 28, 2017

@joshuafcole joshuafcole merged commit 3c4e3e0 into master Jun 28, 2017

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